gpt4 book ai didi

java - 数组列表 intValue()

转载 作者:行者123 更新时间:2023-12-02 06:43:03 27 4
gpt4 key购买 nike

嘿,我有一个数字数组列表,当我尝试转换为 int 时,我得到 The method intValue() is undefined for the type Object 错误。这是代码片段。

while (1>0) {
a = Integer.parseInt(in.next());
if(a == -999)
break;
else {
list.add(a);
i++;
}
}
int j;
int[] array = new int[list.size()];
for(j=0;j<list.size();j++) {
array[j] = list.get(j).intValue();
}

最佳答案

您似乎创建了一个对象列表,而不是整数。因此,当您调用 intValue 时,它会说 Object 类型没有这样的方法。我建议您将列表定义为使用泛型的整数列表。这是示例:

List<Integer> list = new ArrayList<Integer>();

如果您不这样做,那么您创建的列表将包含 Object 类的项目。然后你需要将从列表中获取的对象每次转换为Integer

关于java - 数组列表 intValue(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18929098/

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