gpt4 book ai didi

java.lang.ClassCastException :[I cannot be cast to java. lang.Integer

转载 作者:行者123 更新时间:2023-11-30 02:45:02 26 4
gpt4 key购买 nike

public class Solution {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int l1=Integer.parseInt(br.readLine());int count=0;
String l2=br.readLine();
String[] a=l2.split(" ");int[] no=new int[l1];
for (int i=0;i<l1;i++) {
no[i]=Integer.parseInt(a[i]);
}
List list=Arrays.asList(no);
Set<Integer> set=new LinkedHashSet<Integer>(list);
***for (int integer : set) {***
count=Math.max(count, Collections.frequency(list, integer));
}
}
}

我得到java.lang.ClassCastException:
[我无法在代码的突出显示部分在 Solution.main(Solution.java:23)
处转换为 java.lang.Integer。这是什么原因?

最佳答案

您正在尝试从原始整数数组初始化一个集合。当你这样做时

List list=Arrays.asList(no);

List是无类型的,您构造一个整数数组列表;这绝对不是您正在寻找的,因为您需要 List<Integer> .

幸运的是,这很容易解决:更改 no 的声明至

Integer[] no=new Integer[l1];

并构建list如下:

List<Integer> list = Arrays.asList(no);

其他一切都应该正常工作。

关于java.lang.ClassCastException :[I cannot be cast to java. lang.Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40437481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com