简体   繁体   English

Java Arrays.sort()方法需要一维数组,但我也可以传递一个二维数组,那为什么我不能做int [] a = b(其中b是一个二维数组)?

[英]Java Arrays.sort() method takes 1D arrays, but I can pass a 2D array as well then why can't I do int[] a=b(where b is a 2D array)?

I'm having difficulty understanding the concept that I can pass a 2D array to java's Arrays.sort() method.(I have seen the java docs, the sort method can take only 1D arrays) 我很难理解我可以将2D数组传递给java的Arrays.sort()方法的概念。(我已经看过java文档,sort方法只能采用1D数组)
But then when I try the following code I get error 但是当我尝试以下代码时,我得到错误

int[][] b={{1,2},{2,3}};
int[] a=b;

But the following code works fine 但以下代码工作正常

int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },{ 4, 20, 7 }, { 5, 80, 9 }, };
Arrays.sort(temp, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
    return Integer.compare(o2[1], o1[1]);
}
});

Java doesn't have true 2D arrays. Java没有真正的2D数组。 What it has is a 1D array of 1D arrays. 它具有1D阵列的一维阵列。 This means you can pass int[][][] to Arrays.sort as long as you pass a comparator for int[][] elements. 这意味着只要为int [] []元素传递比较器,就可以将int [] [] []传递给Arrays.sort。

eg 例如

int[][][] a = { ... }
int[][] b = a[0];
int[] c = b[0];
int d = c[0];

You can also write 你也可以写

Object[] x = a; // as int[][] is an Object.
Object[] y = b; // as int[] is an Object.
Object[] z = c; // doesn't compile. as int is not an Object.

The signature of the sort method is - Arrays.sort(T[], Comparator<? super T>) . sort方法的签名是 - Arrays.sort(T[], Comparator<? super T>) When you invoke this method with int[][] and Comparable<int[]> as arguments, type T is inferred as int[] , and that is fine, as an array is an object only. 当使用int[][]Comparable<int[]>作为参数调用此方法时,类型T被推断为int[] ,这很好,因为数组只是一个对象。

However, the first segment of code doesn't compile, because you're assigning an int[][] to an int[] . 但是,第一段代码无法编译,因为您要将int[][]赋值给int[] It is similar to assigning a T type to a T[] array. 它类似于将T类型赋给T[]数组。 That can't be done, as they are incompatible types. 这是不可能的,因为它们是不兼容的类型。

There is no "2D arrays" in Java in the sense that you think. 在你认为的意义上,Java中没有“2D数组”。 An int[][] is an array where each element is an int[] . int[][]是一个数组,其中每个元素都是int[] An int[] is an array where each element is an int . int[]是一个数组,其中每个元素都是一个int

And as int[] and int are different types, your assignment cannot work. 由于int[]int是不同的类型,因此您的赋值不起作用。

You are trying to assign two dimensional array to a single dimensional array reference. 您正在尝试将二维数组分配给单维数组引用。 Java doesn't have 2D arrays. Java没有2D数组。 It has only 1D array or 1D arrays. 它只有1D阵列或1D阵列。 Your assigment reference should be capable of holding the type on the right handside. 您的分配参考应该能够保持右侧的类型。

int[][] b={{1,2},{2,3}};
int[] a=b;

They are incompatible types, cause compilation error. 它们是不兼容的类型,导致编译错误。 You have to correct it like this 你必须像这样纠正它

 int[][] a = b;

You can not place a 2 dimensional array inside a one dimensional array. 您不能在一维数组中放置二维数组。 This 这个

int[][] b={{1,2},{2,3}};
int[] a=b[0];

Will work 将工作

And sorting this will check all elements. 排序这将检查所有元素。

    int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },{ 4, 20, 7 }, { 5, 80, 9 }, };
    Arrays.sort(temp, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        for(int i=0;i<((o1.length<o2.length)?o1.length:o2.length);i++) {
            if (o1[i]!=o2[i])
                return o1[i]-o2[i];
        }
        if (o1.length!=o2.length)
            return o1.length-o2.length;
        return 0;
    }
    });

If you want to sort the internal Arrays also you need to call sort on each Array first, however this will do double work and can be improved. 如果你想对内部数组进行排序,你需要首先在每个数组上调用sort,但这样做会做双重工作并且可以改进。

    int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },{ 4, 20, 7 }, { 5, 80, 9 }, };
    Arrays.sort(temp, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        Arrays.sort(o1);
        Arrays.sort(o2);
        for(int i=0;i<((o1.length<o2.length)?o1.length:o2.length);i++) {
            if (o1[i]!=o2[i])
                return o1[i]-o2[i];
        }
        if (o1.length!=o2.length)
            return o1.length-o2.length;
        return 0;
    }
    });     

Well in your 2D array sorting example, you are comparing two 1D arrays by only checking their second element and comparing them. 在2D数组排序示例中,您只是通过检查第二个元素并比较它们来比较两个1D数组。 (Note that indexes in arrays start from 0). (请注意,数组中的索引从0开始)。 So its bound to work. 所以它必将发挥作用。

Remove that custom comparator, and you'll get a bogus sorted array. 删除该自定义比较器,您将获得一个虚假的排序数组。

But you surely can't refer a primitive 2D array as a primitive 1D array, because the compiler knows their types and will give you error. 但你肯定不能将原始2D数组作为原始1D数组引用,因为编译器知道它们的类型并会给你错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM