跳到主要内容

11.2 数组元素排序

找出整形数组中的最大值:

public static void main(String[] args) {
int[] a = {65, 7, 52, 101, 35, 18, 33};

int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}

System.out.println(max);
}

对整型数组元素进行排序:

冒泡排序:

public static void main(String[] args) {
int[] a = {65, 7, 52, 101, 35, 18, 33};

int temp = 0;
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}