简体   繁体   English

java 2D数组 - 排序和搜索

[英]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. 最近,在为大学做作业时,我遇到的一件事就是整理和搜索2D阵列。 Using Arrays.sort(example); 使用Arrays.sort(example); or Arrays.binarySearch(example, "xyz"); Arrays.binarySearch(example, "xyz"); returns an error: 返回错误:

example cannot be cast to java.lang.Comparable 示例无法强制转换为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. 我不得不使用嵌套for循环,而不是二进制搜索,尽管我希望以有效的方式做到这一点。

Another problem I had was using Arrays.asList(example).contains("xyz"); 我遇到的另一个问题是使用Arrays.asList(example).contains("xyz"); , which never seemed to work. ,似乎从来没有工作过。 I then used System.out.println(Arrays.asList(example)); 然后我使用了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] [[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? 还有一个后续问题 - 为什么有些错误(比如example cannot be cast to java.lang.Comparable只会在程序运行时出现,而不是在编译时出现?

Any help is very much appreciated. 很感谢任何形式的帮助。

java.util.Arrays , except for deepHashCode and deepEquals , does not recursively operate on arrays. 除了deepHashCodedeepEquals之外, java.util.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. 相对于整个阵列对2D阵列进行排序并不是很有意义,最好只将它存储为一维阵列。 The same goes for binary searching a 2D array. 二进制搜索2D数组也是如此。

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 . 最好的方法是让类实现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 . 如果您无法编辑该类或想要提供多种方法对对象进行排序,则可以实现java.util.Comparator并将其实例传递给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 . 为了使用java.util.Arrays对数组进行排序或搜索,数组对象的类型必须实现java.lang.Comparable ,您必须提供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 示例无法强制转换为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. example的项目应该实现Comparable接口并覆盖方法Compare()否则Arrays.sort()不会“知道”按哪个顺序对项目进行排序。

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. 从中实例化example的元素的类也应该重写方法toString() ,以便很好地表示您正在打印的内容。

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

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