Java ArrayList Add Method: Code & Examples

Lesson Transcript
InstructorMartin Gibbs

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 programmers need to understand the method for adding to an ArrayList. Review the method for adding and inserting new elements within a Java ArrayList, including the ArrayList of classes. Updated: 08/24/2023

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:


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

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

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

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 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.

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

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Resources created by teachers for teachers

Over 30,000 video lessons & teaching resources‐all in one place.
Video lessons
Quizzes & Worksheets
Classroom Integration
Lesson Plans

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.

Jennifer B.
Teacher
Jennifer B.
Create an account to start this course today
Used by over 30 million students worldwide
Create an account
close