我有一个包含重复项的整数数组列表,我希望使用HashSet。不幸的是,我无法如愿使用HashSet:
System.out.print("\nTESTs\n");
ArrayList<int[]> list = new ArrayList<int[]>();
list.add(new int[]{1,2,3});
list.add(new int[]{5,1,1});
list.add(new int[]{1,2,3});//duplicate
list.add(new int[]{5,1,3});
Set<int[]> set = new HashSet<int[]>(list);
System.out.println("Size of the set = "+set.size());
ArrayList<int[]> arrayList = new ArrayList<int[]>(set);
System.out.println("Size of the arrayList = "+arrayList.size());
for (int[] array:arrayList){
System.out.println(Arrays.toString(array));
}
它会导致:
Size of the set = 4
Size of the arrayList = 4
[1, 2, 3]
[1, 2, 3] // duplicate still here
[5, 1, 1]
[5, 1, 3]
有人能告诉我错在哪里吗?
先谢谢了。 Dominique(Java 新手)