Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]);
}
});
like image 740
dividedbyzero Avatar asked Feb 23 '14 10:02

dividedbyzero


People also ask

Can we sort 2D array in Java?

In a 2D array, a cell has two indexes one is its row number, and the other is its column number. Sorting is a technique for arranging elements in a 2D array in a specific order. The 2D array can be sorted in either ascending or descending order.

How do you convert a 1D array into 2D?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

How to sort an array in Java?

We can perform sorting in the following ways: In Java, Arrays is the class defined in the java.util package that provides sort () method to sort an array in ascending order. It uses Dual-Pivot Quicksort algorithm for sorting.

Is it possible to sort an array of different types naturally?

NOTES: Some core classes in the JDK already implemented the Comparable interface (e.g. String, Date and primitive wrappers: Integer, Long, Double, etc), so that we can sort array of these types naturally. Here are some examples:

What is an array class in Java?

An array class is a class containing static methods that are used with arrays in order to search, sort, compare, inserting elements, or returning a string representation of an array. in an array. So let us specify the functions first and later onwards we will be discussing the same. They are as follows been present in java.util.Arrays class.

How to sort array in reverse-lexicographic order in Java?

Java Collections class provides the reverseOrder () method to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter.


1 Answers

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.

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.
like image 80
Peter Lawrey Avatar answered Nov 15 '22 05:11

Peter Lawrey