Copyright

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
 Replay
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
Add to Add to Add to

Want to watch this again later?

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

Log in or Sign up

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

ArrayList

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 ArrayList utility


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:


Create Java ArrayList


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:


Java ArrayList populate array


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.


Java arraylist loop through original


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.

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
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 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? Study.com 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
Support