【发布时间】:2013-12-05 03:25:23
【问题描述】:
我有两个 ArrayList,其中又包含 Hardware 类型的 ArrayList:
private ArrayList<ArrayList<Hardware>> list1;
private ArrayList<ArrayList<Hardware>> list2;
Hardware 类包含一个 ElementType 枚举,它决定了 Hardware 的类型:
enum ElementType{
Microcontroller;
Core;
Memory;
Sensor;
Pin;
Port;
}
list1 的示例,其中包含 3 个硬件类型的 ArrayList:
[Microcontroller, Memory]
[Port]
[Pin]
list2 的示例,其中包含 6 个硬件类型的 ArrayList:
[Microcontroller, Memory]
[Microcontroller, Memory]
[Microcontroller, Port]
[Microcontroller, Core]
[Sensor]
[Pin]
现在我想检查列表 1 的所有 ArrayList 是否都包含在 list2 中并且具有相同的顺序:
这将是有效的:
[Microcontroller, Memory, Core] == [Microcontroller, Memory, Core]
虽然这将具有不同的顺序,因此无效:
[Microcontroller, Memory, Core] == [Microcontroller, Core, Memory]
最后,我需要知道 list1 的所有 ArrayList 是否也在 list2 中的顺序相同,以及 list2 中的哪一个。
我为此苦苦挣扎了一段时间,因为我不知道如何为此使用 Collection 的辅助方法,而且我对使用循环的第一个想法非常糟糕并没有帮助我:
private void check() {
for (ArrayList<Hardware> listOf1 : list1) {
for (Hardware abstHw : listOf1) {
System.out.println(abstHw);
????????????
}
}
}
谁能帮我解决这个问题或给我一些方便的提示?
干杯, 菲尔
-------编辑---------------
澄清一下:
list1 和 list2 的硬件实例不一样!它们只能通过 ElementType 进行比较。
【问题讨论】:
标签: java algorithm collections arraylist comparison