Java Arraylist: Learning the Basics

Athena Ozanich
Athena Ozanich

Updated:

Published:

In the world of programming, Java can be a bit different than most other programming languages; in many ways, it's kinda the black sheep of the programming family.

Woman studying Java ArrayLists outside her office building.

Java syntax and code are somewhat different from other languages; for starters, it is a compiled language instead of an interpreted language — but I digress.

While our topic today is Java, we will be focused on the ArrayList class, what it does, and how to use it. We will be going reasonably in-depth on this subject, and by the end of this post, you should feel confident in implementing this class in your coding practices.

Download Now: An Introduction to Java & JavaScript

What is an ArrayList in Java?

An ArrayList is a resizable array from the Java.util package, and it has more flexibility than a standard Java array. In Java, a standard array cannot be resized; that is, array items cannot be added or removed to an array after being created. Instead, if you wish to alter a Java array, you must create a new array instead of the old one.

Enter Java ArrayList. This class provides arrays that are modifiable after creation. One other difference is that the syntax is slightly different, which we will cover in the rest of this post.

How do you use an ArrayList?

Using the ArrayList class is easier than it initially seems, and it comes with a slew of methods for manipulating the arrays. However, to start using the ArrayList class, you need to first import it into the files you need it in.

Import ArrayList Java

Let's look at the syntax and code needed to import the ArrayList class correctly for your project.

Importing is simple and only requires a single line of code to accomplish it:

 
importjava.util.ArrayList;

This line identifies the ArrayList class from the Java.util package and imports it for use in your code. After importing, you will need to set up the main class and a public class, which looks like this:

 
publicclassMain{
publicstaticvoidmain(String[]args){
}
}

Now that we've handled the import, let's go over how to initialize a new ArrayList with the type string.

 
ArrayList<String>colors =newArrayList<String>();

There are other ArrayList types, and they are declared the same way for each type. For each type <Boolean> for boolean, <Character> for char, <Double> for double, etc:

Now that we've created the new ArrayList, let's look at the different methods of the ArrayList class and how to use them.

Java ArrayList Methods

The Java ArrayList comes with various methods to manipulate the contents of your arrays. Let's look at those next, starting with the add method.

Add

The add method allows you to add new items to your ArrayList, greatly benefiting your programs and functionality.

Let's take a look at what that looks like in practice; consider the following:

 
ArrayList<String>colors =newArrayList<String>();
cars.add("Green");
cars.add("Purple");

This code creates an ArrayList of the type string, named colors initialized as an empty array, and then the lines after adding new colors to the array list. At the end of that code, you have an array with the following objects (array items using the ArrayList class are objects, in this case, objects of the string type), "Green" and "Purple."

You can test this with the following line of Java code:

 
System.out.println(colors);

Which will return the following:

 
[Green, Purple]

The other methods all work similarly; let's quickly go over the remaining methods and see how they work.

Get

The get method is straightforward; it gets the value of an ArrayList item and returns it using the index of the array item.

 
colors.get(0);

This method simply returns the array item at the index of 0 (the first item in the array), "Green." Next, let's look at the set method and see how it works.

Set

The set method is used to change an existing item in the array; let's look at that in practice.

 
colors.set(0,"Teal");

This line of code changed the array item at index 0 to the new string "Teal" if you print the array, it will return the following result:

 
[Teal, Purple]

Remove

The remove method will remove an item from the array list based on its index. Let's look at an example of this in action.

 
colors.remove(0);

This line of code is not complicated; it simply targets an ArrayList item based on its index and removes it from the list. This example would successfully remove the item "Teal" from the list.

Next is the clear method that removes all ArrayList items from the array.

Clear

The clear method is more straightforward than the others as it does not take a parameter for the method to work. Instead, you simply need to call the method on your ArrayList array to use the clear method.

 
colors.clear();

If you were to print out the contents of the colors array, you'd find that all the items have been removed from the array.

Size

The size method is used to identify the size of an ArrayList, in other words count the number of array items in it. Let’s look at that next.

 
cars.size();

This line of code would return the size of the colors array; in this case, since we used the clear method it would come back as 0.

Let's look at what the code this far would look like:

 
importjava.util.ArrayList;
publicclassMain{
publicstaticvoidmain(String[]args){
ArrayList<String>colors =newArrayList<String>();
colors.add("Green");
colors.add("Purple");
System.out.println(colors);/*[Green, Purple]*/
System.out.println(colors.get(0))/*Green;*/
System.out.println(colors.set(0,"Teal"));/*Green -> Teal
System.out.println(colors.remove(0));/*[Purple]*/
System.out.println(colors.clear());/*[]*/
System.out.println(cars.size());/*0*/
}
}

Loop Through Java ArrayList

You can iterate much like an array through an ArrayList using a for loop or for-each loop. For example, considering the array created above, looping through it with a for loop would look like the following.

Java for Loop

 
for(inti =0;i <colors.size();i++){
System.out.println(colors.get(i));
}

This code uses the size method to set a maximum constraint for the number of times the loop will run.

Java for-each Loop

 
for(Stringi :cars){
System.out.println(i);
}

This loop isn't much different than the for loop, and the primary difference is that you don't need to dictate the number of times the loop needs to run. Instead, it runs once for each item in the array; let's look at that next.

This line of code will automatically loop through the array once for each item in the list, and then the loop will automatically end.

Final Thoughts on Java ArrayLists

Java ArrayLists are very powerful, and there is a lot that goes into them. When studying new programming language concepts, there is a lot to consider, more than any singular post could ever cover.

Hopefully, this post has helped illuminate Java ArrayLists and guide you in the right direction for furthering your understanding of Java concepts.

java

Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about the differences between and uses of these popular programming languages.