Java-数组常用的排序方法和Arrays常用方法

数组排序

  1. 使用Arrays类中的sort()方法
public static void sortArray_1(int[] array){
    System.out.println("/*第一种排序-Arrays类sort方法*/");
    Arrays.sort(array);
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}
  1. 冒泡排序
public static void sortArray_2(int[] array){
    System.out.println("/*第二种排序-冒泡排序*/");
    int temp;
    for (int i = 0; i < array.length; i++) {
        for (int j = i + 1; j < array.length; j++) {
            if(array[i] > array[j]){
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}
  1. 选择排序
public static void sortArray_3(int[] array){
    System.out.println("/*第三种排序-选择排序*/");
    for (int i = 0; i < array.length; i++) {
        int tem = i;
        /*找到数值最小的下标*/
        for (int j = i + 1 ; j < array.length; j++) {
            if(array[i] > array[j]){
                tem = j;
            }
        }
        /*将最小值下标和i的值进行对换*/
        int temp = array[i];
        array[i] = array[tem];
        array[tem] = temp;
    }
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}
  1. 插入排序
public static void sortArray_4(int[] array){
    System.out.println("/*第四种排序-插入排序*/");
    for (int i = 1; i < array.length; i++) {
        for (int j = i; j > 0; j--) {
            if(array[j-1] > array[j]){
                int temp = array[j-1];
                array[j-1] = array[j];
                array[j] = temp;
            }
        }
    }
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}
  1. 数组反转
public static void sortArray_5(int[] array){
     for (int i = 0; i < array.length; i++) {
         System.out.print(array[i] + " ");
     }
     /*先找其中间值,然后依此为轴,进行数据调换*/
     for (int i = 0; i < array.length/2; i++) {
         int temp = array[i];
         array[i] = array[array.length -i - 1];
         array[array.length -i - 1] = temp;
     }
     System.out.println();
     System.out.println("-----------");
     for (int i = 0; i < array.length; i++) {
         System.out.print(array[i] + " ");
     }
 }

Arrays常用方法

  1. 指定范围排序

复制上边第一种排序方法代码进行修改

public static void main(String[] args) {
    int[] arrays = {3,5,8,1,22,6,9,54};
    sortArray_6(arrays);
}
public static void sortArray_1(int[] array){
    Arrays.sort(array, 0, 5);
    System.out.println("/*第一种排序-Arrays类sort方法*/");
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}

输出结果为:
1 3 5 8 22 6 9 54 
  1. toString()
public static void main(String[] args) {
    int[] arrays = {3,5,8,1,22,6,9,54};
   	int[] arrays = {3,5,8,1,22,6,9,54};
    System.out.println("toString() 结果为:"+ Arrays.toString(arrays));
    /*对于二维数组应当使用deepToString()方法*/
    int[][] arrays2 = {{2,5},{4,2},{5,7}};
    System.out.println("deepToString() 结果为:"+ Arrays.deepToString(arrays2));
}
// 输出结果为: 
// toString() 结果为:[3, 5, 8, 1, 22, 6, 9, 54]
// deepToString() 结果为:[[2, 5], [4, 2], [5, 7]]
  1. 填充数组
int[] arr = new int[5];
Arrays.fill(arr, 2);
/*通过toString() 方法查看结果*/
System.out.println(Arrays.toString(arr));
//输出结果: [2, 2, 2, 2, 2]

int[] arr2 = new int[10];
/*选择下标范围填充*/
Arrays.fill(arr2, 3, 6, 2);
/*通过toString() 方法查看结果*/
System.out.println(Arrays.toString(arr2));
//输出结果为:[0, 0, 0, 2, 2, 2, 0, 0, 0, 0]
  1. 数组复制
int[] arrays = {3,5,8,1,22,6,9,54};
int[] copyarr_1 = Arrays.copyOf(arrays, 10);
int[] copyarr_2 = Arrays.copyOf(arrays, 5);
System.out.println("copyarr_1 = " + Arrays.toString(copyarr_1));
System.out.println("copyarr_2 = " + Arrays.toString(copyarr_2));
//输出结果为:
//copyarr_1 = [3, 5, 8, 1, 22, 6, 9, 54, 0, 0]
//copyarr_2 = [3, 5, 8, 1, 22]
  1. 数组转List
/*使用int包装类Integer作为类型*/
Integer[] arrays = {3,5,8,1,22,6,9,54};
List list = Arrays.asList(arrays);
System.out.println("list.size() = "+list.size());
for (int i = 0; i < list.size(); i++) {
    System.out.print(list.get(i)+ " ");
}
System.out.println();
System.out.println("-------------");
/*使用int作为类型,是将整个数组作为一个集合的值*/
int[] int_arrays = {3,5,8,1,22,6,9,54};
// List int_list = Arrays.asList(int_arrays); /会报错,类型不匹配
List int_list = Arrays.asList(int_arrays);
System.out.println("int_list.size() = "+int_list.size());
for (int i = 0; i < int_list.size(); i++) {
    System.out.println(Arrays.toString(int_list.get(i)));
}
//输出结果为:
// list.size() = 8
// 3 5 8 1 22 6 9 54 
// -------------
// int_list.size() = 1
// [3, 5, 8, 1, 22, 6, 9, 54]

你可能感兴趣的:(java)