有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将二维整数数组的行从最小到最大排序

我有一个名为arry的2D整数数组,如下所示:

[6, 2, 7]
[3, 6, 7]
[5, 6, 1]
[5, 3, 4]
[5, 3, 8]

我想按照结果的方式对其进行排序(为此,我创建了相同大小的新数组,名为table):

[3, 6, 7]
[5, 6, 1]
[5, 3, 4]
[5, 3, 8]
[6, 2, 7]

我有这个密码:

for (int k = 0; k < numOfArrays; k++) {
    int smallest = 2147483647;
    int indexSmallest = 0;
    for (int h = 0; h < numOfArrays; h++) {
        if (arry[h][0] < smallest) {
            smallest = arry[h][0];
            indexSmallest = h;
        }
    }
    tabel[k] = arry[indexSmallest];
    arry[indexSmallest][0] = 2147483647;
}

for (int k = 0; k < numOfArrays; k++) {
    System.out.println(Arrays.toString(tabel[k]));
}

结果是:

[2147483647, 6, 7]
[2147483647, 6, 1]
[2147483647, 3, 4]
[2147483647, 3, 8]
[2147483647, 2, 7]

如果我从未将表的任何值设置为2147483647,我不明白表怎么能包含2147483647


共 (4) 个答案

  1. # 1 楼答案

    您已经在对象内部创建了此信息,并实现了Comparable/Comparator:

    public YourObject implements Comparable {
        private long a;
        private long b;
        private long c;
    
        public YourObject (long a, long b, long c){
             this.a = a;
             this.b = b;
             this.c = c;
        }
    
        public int compareTo(Object arg0) {
            //make your comparison here...
        }
    
    }
    

    主要功能: YourObject[]arr=新建YourObject[3]; arr[0]=新的YourObject(…,…); arr[1]=新的YourObject(…,…); arr[2]=新的YourObject(

    数组。排序(arr)//整理数据

  2. # 2 楼答案

    真正的问题是这句话:

    tabel[k] = arry[indexSmallest];
    

    记住,数组是对象。此行不复制内部数组,而是设置对它的引用

    所以此时tabel[k]arry[indexSmallest]都指向同一个数组对象。所以当你这么做的时候:

    arry[indexSmallest][0] = 2147483647;
    

    您可以为arry[indexSmallest]tabel[k]更改它(因为它们指向同一个对象)

    要解决此问题,请将数组的tabel[k]a复制为

    tabel[k] = Arrays.copyOf(arry[indexSmallest], 3);
    
  3. # 3 楼答案

    可以通过使用^{}方法大大简化代码,或者可以实现类似的功能


    按列对2d数组进行选择排序

    public static void selectionSort2D(int[][] arr, int column) {
        // iterate over all subsets of the rows of the array
        // (0-last, 1-last, 2-last, 3-last, ...)
        for (int i = 0; i < arr.length; i++) {
            // assume the min is the first row element
            int min = arr[i][column];
            // row index of the min element
            int min_i = i;
            // check the rows after i to find the smallest
            for (int j = i + 1; j < arr.length; j++) {
                // if this row element is less,
                // then it is the new min
                if (arr[j][column] < min) {
                    min = arr[j][column];
                    min_i = j;
                }
            }
            // if the min element row is not equal to
            // the current one, then swap these rows
            if (i != min_i) {
                int[] temp = arr[i];
                arr[i] = arr[min_i];
                arr[min_i] = temp;
            }
        }
    }
    
    // test
    public static void main(String[] args) {
        int[][] arr = {
                {6, 2, 7},
                {3, 6, 7},
                {5, 6, 1},
                {5, 3, 4},
                {5, 3, 8}};
    
        // sort by first column
        selectionSort2D(arr, 0);
    
        // output
        for (int[] row : arr)
            System.out.println(Arrays.toString(row));
        //[3, 6, 7]
        //[5, 6, 1]
        //[5, 3, 4]
        //[5, 3, 8]
        //[6, 2, 7]
    }
    
  4. # 4 楼答案

    记住,Java数组是对象,这意味着它们是通过引用传递的。所以你并不是在复制内部数组;用Arrays.copyOf()来表示

    如果允许将Arrays.sort()Comparator一起使用,可以这样做

    java.util.Arrays.sort(arry, new java.util.Comparator<int[]>() {
        public int compare(int[] a1, int[] a2) {
            for (int k = 0; k < a1.length; k++) {
                if (a1[k] != a2[k]) {
                    return a1[k] - a2[k];
                }
            }
            return 0;
        }
    });
    for (int k = 0; k < numOfArrays; k++) {
        System.out.println(java.util.Arrays.toString(arry[k]));
    }
    

    如果这是不允许的,那么您仍然可以在上面的Comparator.compare()方法中使用比较逻辑。这是基本的排序逻辑,与实现细节无关