Martin has 22 years experience in Information Systems and Information Technology, has a PhD in Information Technology Management, and a master's degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.
Java ArrayList Add Method: Code & Examples
Standard arrays are useful tools in the Java programmer's toolbox. There are times when we need something a little more dynamic, an array that we can add and/or subtract from, or even grow or shrink as the need arises. Thankfully, Java provides the ArrayList, which is a class for us to use when we need to do that.
Before we can use ArrayList we have to import the Java utility, as you can see below.
import java.util.ArrayList;
In order to create a new array list, you create an instance of the ArrayList class, telling Java the type of data in the array and the size of the array. The array can be String, Integer, Double, etc.
Below is the code to declare an instance of the ArrayList class and call it employees. It's a String array with five items, as you can see:
ArrayList <String> employees = new ArrayList <String>(5);
Now we can start using the ArrayList class and its add method to insert items into the array. At first, we're going to create the list using the add method. Later, we will actually tell Java what to insert into the array, and at what position.
The code to populate our array is below, as you can see, with names of famous fictional characters:
employees.add("Jane Eyre");
employees.add("Sherlock Holmes");
employees.add("Edmond Dantes");
employees.add("Jean Valjean");
employees.add("Sam Spade");
The next lines of code use the size method of ArrayList to display the number of items. Next we loop through the array and display each item. You'll notice that this for loop is a little different than others. Because we have an ArrayList, we can take a shortcut on our loop. We only need to specify the data type of the ArrayList (String), give a counter variable name (counter), and the name of the ArrayList (employees).
The add method of ArrayList can take up to two parameters. The first is the index where you are inserting. The second is the value of the item being inserted. When you add an item to the ArrayList it shifts everything over.
This highlights the beauty of the ArrayList versus a standard array. Not only can we insert a value into a specific point of the array, it actually increases the size of the array.
//show arraylist size
System.out.println("Size = " + employees.size());
//loop through list and display
for (String counter : employees) {
System.out.println("Employee = " + counter);
}
When run, the output is as follows, in which our famous characters are listed as employees:
![]() |
If you don't specify an index, Java simply appends the item at the end of the list. It then increases the size by 1.
To unlock this lesson you must be a Study.com Member.
Create your account
We now have a decent String array with some interesting employees. After this has been created, we may need to add new employees to our list. If we had been using standard arrays, it would be harder to add an item while keeping the same list. You have to create a new array and copy everything to the new one. Instead, we'll use the add function and add/insert a new element.
In order to add an element to an existing array, we will use the parameters for the add function; the index where we will insert the value, and the actual value. Remember that Java starts counting array elements at zero. Therefore, bucket 1 is the second element. The following code will add our new employee into the second position:
employees.add(1, "Anna Karenina");
This will add Anna Karenina into the second position of the array (bucket 1 to Java). But what happens to the rest of the array? Let's loop through the array again, displaying the size of the array each time. Below is the full code, showing the first array, our addition, and a second loop through it:
public class Main {
public static void main(String[] args) {
//create an instance of ArrayList
employees.add("Jane Eyre");
employees.add("Sherlock Holmes");
employees.add("Edmond Dantes");
employees.add("Jean Valjean");
employees.add("Sam Spade");
//show arraylist size
System.out.println("Size = " + employees.size());
//loop through list and display
for (String counter : employees) {
System.out.println("Employee = " + counter);
System.out.println();
//add Anna Karenina at 2nd position
//remember Java starts at 0
System.out.println("Array after Add: ");
employees.add(1, "Anna Karenina");
for(String counter : employees) {
System.out.println("Employee = " + counter);
}
System.out.println("Size = " + employees.size());
}
}
And the output. Take note of the size of the array, and how Java nicely shifts everything over.
![]() |
To unlock this lesson you must be a Study.com Member.
Create your account
Another use for the ArrayList add function is for a list of classes. Let's say you create an Artist class and want to store a list of Artist objects (objects are instances of classes) into an ArrayList. Let's say the Artist class requires an artist ID and a name. We can use the add method of ArrayList to insert instances of the Artist class into the array.
The following code declares the new ArrayList. Notice that our data type is actually Artist. That is because we are creating objects of type Artist. Next, we'll insert a couple of artists.
//*** ArrayList of Classes
ArrayList <Artist> artist = new ArrayList < > ();
artist.add(new Artist(1025, "REO Speedwagon"));
artist.add(new Artist(9067, "Meat Loaf"));
The add function of ArrayList has many uses. ArrayList can be much easier to work with than standard arrays.
To unlock this lesson you must be a Study.com Member.
Create your account
The ArrayList is a class in Java and is a powerful tool for working with arrays. When you create an instance of the ArrayList class, you can use several methods within the class for array processing. The add method lets you insert or add an element into the array at a specified position. Instead of replacing the element already there, it shifts the array buckets over, thus increasing the size of the array. To see the size of the ArrayList use the size method. For this reason, it's a superior tool for arrays, especially if arrays will be dynamic.
To unlock this lesson you must be a Study.com Member.
Create your account
Register to view this lesson
Unlock Your Education
See for yourself why 30 million people use Study.com
Become a Study.com member and start learning now.
Become a MemberAlready a member? Log In
BackResources created by teachers for teachers
I would definitely recommend Study.com to my colleagues. It’s like a teacher waved a magic wand and did the work for me. I feel like it’s a lifeline.