【发布时间】:2017-02-18 10:35:20
【问题描述】:
我把代码写成:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> has1 = new HashSet(Arrays.asList(nums1));
for (int i: has1)
System.out.println(i);
return nums1;
}
}
num1: [1,2,4,2,3]
num2: [4,5,6,3]
在 for 循环中显示 java.lang.ClassCastException: [I cannot be cast to java.lang.Integer
【问题讨论】:
-
int和Integer不是同一类型。修复for中的类型,它应该可以工作。 -
好的,谢谢!我已经根据这个想法编写了这段代码。但是我的代码中有一个错误。我想知道如何解决它。
-
你需要像
new HashSet<>(IntStream.of(nums1).boxed().collect(Collectors.toList()))这样的东西,你目前正在获得HashSet<int[]>并使用原始类型,所以你也忽略了警告。 -
@4castle Raw types.
标签: java loops for-loop hashset