Find a mistake

Report a typo

Create a file List.java and copy the following code into it:

import java.util.ArrayList;
import java.lang.Integer;
public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        for (int i = 0; i < 5; i++) {
            nums.add(i);
        }
        System.out.println(nums.get(5));
    }
}

The java.lang.IndexOutOfBoundsException: Index: 5, Size: 5 error occurs when running the code. In which line of code does this error occur?

Select one or more options from the list
___

Create a free account to access the full topic