Below is the code that I wrote:
import static java.lang.System.out;
import java.util.Scanner;
import java.util.ArrayList;
class Uni{
static public void main(String...args){
Scanner sc = new Scanner(System.in);
ArrayList<Integer>list = new ArrayList<Integer>();
for(int a=0,i=0;list.get(i)!=42;i++){
a=sc.nextInt();
list.add(i,a);
}
for(int i=0;i<list.size();i++){
out.println(list.get(i));
}
}
}
And this is the error im getting:
Execution failed. java.lang.IndexOutOfBoundsException : Index: 0, Size: 0
Stack Trace: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at Uni.main(Uni.java:8)
Can you please help with what to do?
list.get(i)!=42to do when the list is empty?