数组总结及冒泡排序
反转数组
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
for (int i = 0 , j = result.length-1 ; i < arrays.length ; i++ , j--) {
result[j] = arrays[i];
}
return result;
}
二维数组
public static void main(String[] args) {
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
System.oAut.println(array.length);
System.out.println(array[0].length);
System.out.println("============================");
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j<array[i].length;j++){
System.out.println(array[i][j]);
}
}
}
冒泡排序
public static void main(String[] args) {
int[] arrays = {15,55,78,0,654,21,65};
int[] sort = sort(arrays);
System.out.println(Arrays.toString(sort));
}
public static int[] sort(int[] array){
int temp = 0;
for (int i = 0; i <array.length-1 ; i++) {
boolean flag = false;
for (int j = 0;j<array.length-1-i;j++){
if (array[j]>array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
if (flag == false){
break;
}
}
return array;
}