gpt4 book ai didi

Java - 长列表错误 : incompatible types

转载 作者:行者123 更新时间:2023-12-01 18:01:45 30 4
gpt4 key购买 nike

我正在编写这段使用大数字的代码:

import java.math.*;
import java.util.*;
import java.lang.*;

public class main {
public static void main(String[] args){
double max = 1e+15;
List<Integer> sum = new ArrayList<Integer>();
sum.add(1);
long aux = 0;
for(long i = 1;i<max;i++){
sum.add(sum.get(i-1)+scomponi(sum.get(i-1),aux));
if(i+1==1e+7 || i+1==1e+8 || i+1==1e+9 || i+1 == 1e+10 || i+1 == 1e+10 || i+1==1e+11 || i+1==1e+12 || i+1==1e+13 || i+1==1e+14)
System.out.println(i+1+"-imo -> "+sum.get(i));
}
System.out.println(sum.get(sum.size()-1));
}


public static Long scomponi(Long num,Long tot) {
while (num > 0) {
Long digit = num % 10;
tot += digit;
num /= 10;
}
return tot;
}
}

但我无法找到此错误的解决方案:

error

这是什么意思?我不太擅长java,但是代码这么简单,有什么问题吗?

最佳答案

Java 中的列表是 int 索引的,正如您在 List.get() 的 javadoc 中看到的那样,并且您正在尝试通过 long 索引获取元素。

如果您需要大于整数范围的列表,则需要另一个支持它的数据结构,则不能使用List

关于Java - 长列表错误 : incompatible types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40395727/

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