【发布时间】:2015-02-05 12:44:02
【问题描述】:
因为我有一个包含重复的 int 数组的 ArrayList,所以我想使用 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]
谁能告诉我哪里错了?
提前致谢 多米尼克(java 新手)
【问题讨论】: