简体   繁体   English

用Java对2d对象数组进行排序(java.lang.Object)

[英]Sorting a 2d array of Objects in Java (java.lang.Object)

I have an ArrayList: 我有一个ArrayList:

ArrayList<Object[][]> parents;

Where column 1 contains an ArrayList of Integers and column 2 contains a single int. 其中列1包含整数的ArrayList,列2包含单个int。 I have cased parents as an ArrayList of Objects as the two objects inside it derive from Class Objects, rather than converting. 我已经将父母作为对象的ArrayList进行了案例展示,因为它里面的两个对象是从类对象派生的,而不是进行转换的。

This is where I've gotten a problem, now I need to sort parents by column two, the ints, but have ran into the issue with using java.util.Arrays.sort as I get the following error: 这是我遇到的问题,现在我需要按第二列int对父级进行排序,但是在遇到以下错误时遇到了使用java.util.Arrays.sort的问题:

The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (ArrayList<Object[][]>, new Comparator<Integer[]>(){})

is there any way around this to cast that second column as an Integer? 有什么办法可以将第二列转换为整数? The error us coming from the ArrayList containing the more general Object 我们来自包含更一般对象的ArrayList的错误

If this isn't possible, what is the best way to go about changing in the code so the array can be sorted? 如果这不可能,那么更改代码以便对数组进行排序的最佳方法是什么?

Please don't mark as duplicate from the sorting of int[][] questions as I haven't seen this issue on here before. 请不要将int [] []问题的排序标记为重复,因为我以前在这里没有看到过此问题。

Thank you! 谢谢!

EDIT - as requested this is an example of my ArrayList 编辑-根据要求,这是我ArrayList的一个示例

[50, 50, 50, 100, 50, 80, 50], 925
[80, 100, 100, 50, 80, 100], 1300
[50, 100, 100, 50, 100, 100], 1200

Ignoring the first column, I'd like to sort it by the second, like so: 忽略第一列,我想按第二列对其进行排序,如下所示:

[50, 50, 50, 100, 50, 80, 50], 925
[50, 100, 100, 50, 100, 100], 1200
[80, 100, 100, 50, 80, 100], 1200

For context the first column is an array referencing items a player has in a game, and the second column is their cost (smaller is better). 对于上下文,第一列是引用玩家在游戏中拥有的物品的数组,第二列是其成本(越小越好)。 I'd like to sort the players by this cost column (2), and of course their items will move with them to the same sorted row. 我想按此费用列(2)对玩家进行排序,当然,他们的项目也将与他们一起移至相同的排序行。 The results are sorted smallest first, so in a tournament selection (this is a genetic algorithm), I can 'kill' the players with the highest costs by trimming the ArrayList. 结果排在最小前,因此在锦标赛选择中(这是一种遗传算法),我可以通过修剪ArrayList来“杀死”成本最高的玩家。

First of all, this is not really the right way to do this. 首先,这不是真正正确的方法。 As @JB Nizet mentioned in the comments, you should create a class with appropriate members and then create a List of that class. 正如@JB Nizet在评论中提到的那样,您应该创建一个具有适当成员的类,然后创建该类的List The elements of those lists then can be compared in a safe way. 然后可以安全地比较那些列表的元素。

But if you should do it this way, here is a solution: 但是,如果您应该这样做,这里是一个解决方案:

parents.sort(new Comparator<Object[][]>() {
            @Override
            public int compare(Object[][] o1, Object[][] o2) {
                return Integer.compare((int) o1[0][1], (int)o2[0][1]);
            }
        });

The error message you have reported is actually directing you to create a comparator which is compatible with the object being passed to sort . 您报告的错误消息实际上是在指导您创建一个与要传递给sort的对象兼容的比较器。

This is not safe at all. 这一点都不安全。 The objects need to exactly have the format you have indicated they would have, otherwise all hell will break loose. 这些对象必须完全具有您指定的格式,否则所有的事情都会变得松散。 Of course, you can add checks to the overriden compare method to ensure format is correct. 当然,您可以将检查添加到覆盖的比较方法中,以确保格式正确。 But why burden yourself with all that complexity when you can use simple OO design patterns and have a nice meaningful class to solve your problem? 但是,当您可以使用简单的OO设计模式并拥有一个很好的有意义的类来解决您的问题时,为什么还要负担这么多的复杂性呢?

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

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