简体   繁体   中英

java 2D Arrays - sorting and searching

Recently, when doing an assignment for college, one of the things I came across was sorting and searching a 2D-array. Using Arrays.sort(example); or Arrays.binarySearch(example, "xyz"); returns an error:

example cannot be cast to java.lang.Comparable

Instead of binary search, I had to use nested for loops, though I would have liked to do it the efficient way.

Another problem I had was using Arrays.asList(example).contains("xyz"); , which never seemed to work. I then used System.out.println(Arrays.asList(example)); to check what was wrong. This is what I got:

[[I@1575c7e, [I@795f24, [I@554210, [I@16433e4, [I@18ada25, [I@f7bd29, [I@a3cf3e, [I@7af3e0, [I@21151e, [I@1f194d9]

What is that and most importantly what is the best way to get around these problems?

And a follow up question - Why do some errors (like example cannot be cast to java.lang.Comparable only appear once the program is run, not when it is compiled?

Any help is very much appreciated.

java.util.Arrays , except for deepHashCode and deepEquals , does not recursively operate on arrays.

Ex:

int[ ][ ] arr = new int[ h ][ w ];
Arrays.sort( arr ); \\ Sorts int[ ] not ints
Arrays.asList( arr ); \\ Returns List< int[ ] >

Sorting a 2D array with respect to the entire array doesn't make a whole lot of sense and you would be better off just storing it as a 1D array. The same goes for binary searching a 2D array.

If you want to sort objects there are two ways to do it. The preferable way is to have the class implement java.lang.Comparable . If you can not edit the class or want to provide multiple ways to sort an object you can implement java.util.Comparator and pass an instance of it to Arrays.sort .

In order to sort or search an array using java.util.Arrays the type of the object of the array must either implement java.lang.Comparable of you must provide an instance of java.util.Comparator . Remember this must be the same type as the type of the object found at the first level of the array.

Ex:

int[ ]       arr1 = ...; \\ Type of first level is int
int[ ][ ]    arr2 = ...; \\ Type of first level is int[ ]
int[ ][ ][ ] arr3 = ...; \\ Type of first level is int[ ][ ]

First problem :

example cannot be cast to java.lang.Comparable

The items in example should implement Comparable interface and override the method Compare() otherwise Arrays.sort() wouldn't "know" by which order to sort your items.

Second problem , printing displays as:

[[I@1575c7e, [I@795f24, [I@554210, [I@16433e4, [I@18ada25, [I@f7bd29, [I@a3cf3e, [I@7af3e0, [I@21151e, [I@1f194d9]

The class from which you instantiate the elements in example should also override the method toString() in order to get a nice representation of what you're printing.

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