Teshtek Teshtek - 1 month ago 7
Java Question

Java - Long List ERROR : incompatible types

I'm working on this code, that use big numbers :

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;
}
}


But I'm not able to find a solution for this error :

error

What it means? I'm not really good in java, but this is so simple code, what's wrong?

Answer

Lists in Java are int-indexed, as you can see in the javadoc of List.get(), and you're trying to get elements by a long index.

If you need lists bigger than the integer range, you need another data structure that supports that, you cannot use List.

Comments