小编Han*_* Li的帖子

用于循环来访问HashSet(Java)中的所有元素?

我把代码编写为:

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]
Run Code Online (Sandbox Code Playgroud)

在for循环中它说 java.lang.ClassCastException: [I cannot be cast to java.lang.Integer

java loops for-loop hashset

1
推荐指数
1
解决办法
5800
查看次数

标签 统计

for-loop ×1

hashset ×1

java ×1

loops ×1