Java ArrayList Add Method: Code & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: ArrayList Get Method in Java: Code & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 ArrayList
  • 2:18 Adding & Inserting New…
  • 3:26 ArrayList of Classes
  • 4:08 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

The power of ArrayList (versus a standard array) is that we can add members to the list. This lesson will define the ArrayList add function and provide working 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:

Java arraylist loop through output

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.

Adding & Inserting New Elements

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");

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account