【发布时间】:2020-02-22 17:09:24
【问题描述】:
public static String duplicate(int[] numbers){
Set<Integer> set = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();
for (int num : numbers){
if (set.contains(num)) {
duplicates.add(num);
} else {
set.add(num);
}
}
int[] ans = duplicates.toArray();
return ans.sort().toString();
}
Eclipse 对 int[] ans = duplicates.toArray(); 行显示“类型不匹配:无法从 Object[] 转换为 int[]”
为什么不呢?我也尝试了 LinkedList 和 ArrayList。 HashSet() 使用类型,而 .toArray() 函数是否认为返回将是 Object[]?
【问题讨论】:
标签: java arrays generics types integer