简体   繁体   中英

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)
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. 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.

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>) . 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.

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.

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 .

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

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.

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.

    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. (Note that indexes in arrays start from 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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