We can also insert new elements into an ArrayList.

To insert new elements into an ArrayList, we can use a slightly different version of the add method that you previously used:

ArrayList<Integer> quizGrades = new ArrayList<Integer>(); quizGrades.add(95); quizGrades.add(87); quizGrades.add(73); quizGrades.add(0, 100); System.out.println( quizGrades.get(0) );

The example above will print out the grade 100.

The add method will insert the grade 100 at the first position (0) into the list. Since it inserts a new element into the beginning of the ArrayList, all other element indices will be shifted one position higher. The grade 95 is now at index 1.



Insert a new temperature of 111 at index 2.


On the next line, use the get method to print out the temperature 89.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?