由于我有一个包含重复项的int数组的ArrayList,我想使用HashSet。不幸的是,我不能管理,因为我想使用HashSet的:int数组的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]
有谁告诉我,我错了?
预先感谢 多米尼克(java的新手)
好的。我现在更好地理解。感谢大家的迅速回答 – Dominique 2015-02-05 14:29:12