Learn Java: ArrayLists
Adding an Item

Now we have an empty ArrayList, but how do we get it to store values?

ArrayList comes with an add() method that takes an argument to add to the end of the ArrayList:

ArrayList<Integer> sudokuRow1 = new ArrayList<Integer>(); sudokuRow1.add(4); // sudokuRow1 now holds [4] sudokuRow1.add(8); // sudokuRow1 now holds [4, 8] sudokuRow1.add(3); // sudokuRow1 now holds [4, 8, 3]



We’ve created an empty ArrayList called toDoList. Time to add some to-dos!

Below where we’ve initialized toDo1, initialize two new String variables: toDo2 and toDo3.

Set their values to any tasks you like.


Use .add() to add toDo1, toDo2, and toDo3 to toDoList.

Folder Icon

Take this course for free

Already have an account?