Teshtek Teshtek - 1 year ago 75
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>();
long aux = 0;
for(long i = 1;i<max;i++){
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));

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 :


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download