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

时间:2021-06-10 21:18:02

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)
But then when I try the following code I get error

我很难理解我可以将2D数组传递给java的Arrays.sort()方法的概念。(我已经看过java文档,sort方法只能采用1D数组)但是当我尝试下面的代码时我得到错误

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]);
}
});

6 个解决方案

#1


5  

Java doesn't have true 2D arrays. What it has is a 1D array of 1D arrays. This means you can pass int[][][] to Arrays.sort as long as you pass a comparator for int[][] elements.

Java没有真正的2D数组。它具有1D阵列的一维阵列。这意味着只要为int [] []元素传递比较器,就可以将int [] [] []传递给Arrays.sort。

e.g.

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.

#2


4  

The signature of the sort method is - 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.

sort方法的签名是 - Arrays.sort(T [],Comparator )。当使用int [] []和Comparable 作为参数调用此方法时,类型T被推断为int [],这很好,因为数组只是一个对象。

However, the first segment of code doesn't compile, because you're assigning an int[][] to an int[]. It is similar to assigning a T type to a T[] array. That can't be done, as they are incompatible types.

但是,第一段代码无法编译,因为您将int [] []赋值给int []。它类似于将T类型赋给T []数组。这是不可能的,因为它们是不兼容的类型。

#3


2  

There is no "2D arrays" in Java in the sense that you think. An int[][] is an array where each element is an int[]. An int[] is an array where each element is an int.

在你认为的意义上,Java中没有“2D数组”。 int [] []是一个数组,其中每个元素都是int []。 int []是一个数组,其中每个元素都是int。

And as int[] and int are different types, your assignment cannot work.

由于int []和int是不同的类型,因此您的赋值不起作用。

#4


1  

You are trying to assign two dimensional array to a single dimensional array reference. Java doesn't have 2D arrays. It has only 1D array or 1D arrays. Your assigment reference should be capable of holding the type on the right handside.

您正在尝试将二维数组分配给单维数组引用。 Java没有2D数组。它只有1D阵列或1D阵列。您的分配参考应该能够保持右侧的类型。

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;

#5


1  

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;
    }
    });     

#6


0  

Well in your 2D array sorting example, you are comparing two 1D arrays by only checking their second element and comparing them. (Note that indexes in arrays start from 0). So its bound to work.

在2D数组排序示例中,您只是通过检查它们的第二个元素并比较它们来比较两个1D数组。 (注意,数组中的索引从0开始)。所以它必将发挥作用。

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数组,因为编译器知道它们的类型并会给你错误。

#1


5  

Java doesn't have true 2D arrays. What it has is a 1D array of 1D arrays. This means you can pass int[][][] to Arrays.sort as long as you pass a comparator for int[][] elements.

Java没有真正的2D数组。它具有1D阵列的一维阵列。这意味着只要为int [] []元素传递比较器,就可以将int [] [] []传递给Arrays.sort。

e.g.

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.

#2


4  

The signature of the sort method is - 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.

sort方法的签名是 - Arrays.sort(T [],Comparator )。当使用int [] []和Comparable 作为参数调用此方法时,类型T被推断为int [],这很好,因为数组只是一个对象。

However, the first segment of code doesn't compile, because you're assigning an int[][] to an int[]. It is similar to assigning a T type to a T[] array. That can't be done, as they are incompatible types.

但是,第一段代码无法编译,因为您将int [] []赋值给int []。它类似于将T类型赋给T []数组。这是不可能的,因为它们是不兼容的类型。

#3


2  

There is no "2D arrays" in Java in the sense that you think. An int[][] is an array where each element is an int[]. An int[] is an array where each element is an int.

在你认为的意义上,Java中没有“2D数组”。 int [] []是一个数组,其中每个元素都是int []。 int []是一个数组,其中每个元素都是int。

And as int[] and int are different types, your assignment cannot work.

由于int []和int是不同的类型,因此您的赋值不起作用。

#4


1  

You are trying to assign two dimensional array to a single dimensional array reference. Java doesn't have 2D arrays. It has only 1D array or 1D arrays. Your assigment reference should be capable of holding the type on the right handside.

您正在尝试将二维数组分配给单维数组引用。 Java没有2D数组。它只有1D阵列或1D阵列。您的分配参考应该能够保持右侧的类型。

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;

#5


1  

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;
    }
    });     

#6


0  

Well in your 2D array sorting example, you are comparing two 1D arrays by only checking their second element and comparing them. (Note that indexes in arrays start from 0). So its bound to work.

在2D数组排序示例中,您只是通过检查它们的第二个元素并比较它们来比较两个1D数组。 (注意,数组中的索引从0开始)。所以它必将发挥作用。

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数组,因为编译器知道它们的类型并会给你错误。