HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

8) Arrays & ArrayLists Lesson

What is a Java ArrayList?

12 min to complete · By Ryan Desmond

In Java, an ArrayList is very useful. ArrayLists are similar to standard Arrays, but they differ in that they do not have a fixed size, they have a number of useful helper methods, and how you interact with them is slightly different.

How Does the Java ArrayList Work?

An ArrayList is a resizable array (or resizable data structure) implementation of the List interface. It implements all optional List operations.

The ArrayList class provides methods to manipulate the size of the array used internally to store the list. This means that ArrayLists can grow and shrink dynamically as data is added and removed. You do not need to declare the size of an ArrayList when you're creating it, as you do when creating a traditional Array.

How to Create an ArrayList?

Creating an ArrayList is similar to what you learned when creating an array: you must declare the type and name. However, the size is not required. As a result, the syntax for creating an ArrayList is as follows:

ArrayList<type> name = new ArrayList();

The ArrayList class requires you to declare the type of value contained within it, which is done with the diamond operators: <>.

Java ArrayList Example

That was a lot of theory, so how about you look at this example that shows a real ArrayList in action?

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // create new ArrayList of String  
    ArrayList<String> list = new ArrayList();
    // add values to ArrayList
    list.add("Hello");
    list.add("World");
    list.add("!");
    // if the list is not empty
    if (!list.isEmpty()){
      // iterate over every element in the ArrayList and print it
      for (String s : list){
        System.out.print(s + " ");
      }
    }
  }
}

Well, that example might have provided you with many new questions since multiple things are going on there that you haven't seen yet, starting with the first line!

How to Import in Java

The first line uses the import statement since the ArrayList is not automatically added to a new file - you have to tell the file to grab the class from where you say it is. The import statement isn't just used for classes and methods; more on this later.

Since ArrayList is built into the util class (along with many other functionalities), you only need one line to start using an ArrayList in your file: import java.util.ArrayList;.

Next, two functions are used on the list variable: .add() and isEmpty(). Just like with arrays, an ArrayList comes built-in with a bunch of ready-to-use functions.

What are the Most Used ArrayList Functions?

The ArrayList can be considered a wrapper class that builds upon Arrays, giving it extra functionality. Some of the most useful methods of an ArrayList are:

Type Name Description
boolean add(E element)
Appends the specified element to the end of the list.
void add(int index, E element)
Inserts the specified element at the specified position in the list.
void clear()
Removes all of the elements from the list.
Object clone()
Returns a shallow copy of this ArrayList instance.
E get(int index)
Returns the element at the specified position in the list.
int indexOf(Object o) Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.
 boolean  isEmpty()  
Returns true if the list contains no elements.
 E  remove(int index)  
Removes the element at the specified position in the list.
 int  size()  
Returns the number of elements in the list.

As you can see, ArrayLists and Arrays are similar but not quite the same. So what are the differences?

What are the Differences Between Java Array and Java ArrayList?

Several differences exist between Arrays and ArrayLists in Java. One of the significant benefits of Arrays is that the size is defined in its declaration, which means that it doesn't have a dynamic capacity that results in taking up more space in your hard drive.

Arrays also can be multidimensional and handle primitive types, whereas an ArrayList is single-dimensional and can exclusively handle Object types (this is because they are Generic and require the diamond operators to define their type).

But don't let this lead you to believe that Arrays are always better! An ArrayList has a lot of added functionality, such as the following methods:

  • add()
  • remove()
  • replace()
  • isEmpty()
  • and more!

For more information about ArrayLists, check out the oracle documentation.

Experiment with Java ArrayLists

In the code editor below, please write the code to:

  • Declare and initialize an ArrayList
  • Add values to an ArrayList
  • Retrieve values from an ArrayList
  • Remove values from an ArrayList
  • Additionally, demonstrate the use of at least two other helper methods provided by the ArrayList class
import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    
    // demonstrate how to declare and initialize ArrayList
  
    // demonstrate how to add values to an ArrayList
  
    // demonstrate how to retrieve values from an ArrayList

    // demonstrate how to remove values from an ArrayList
  
    // demonstrate at least two other useful methods available
    // to us when we use ArrayLists
  

    // keep your code above this line
  }
}

Summary: What is a Java ArrayList?

  • An ArrayList is like an array but without a fixed size
  • An ArrayList is an implementation of the List interface
  • An ArrayList has a capacity that is used to store the data structure, and it automatically changes relative to the size of the values it holds
  • The import statement is used to import classes and methods that are not automatically added to your file
  • An ArrayList comes with a long list of built-in functionalities

Most Used Java ArrayList Methods

  • add() is used to add new elements to the ArrayList
  • clear() is used to remove all elements from the ArrayList
  • clone() is used to create a copy of the ArrayList
  • get() is used to get a specific element from the ArrayList
  • indexOf() is used to get the index of an element in the ArrayList
  • isEmpty() is used to check whether an ArrayList is empty
  • remove() is used to remove an element from the ArrayList
  • size() is used to check the total size of an ArrayList

Main Differences Between ArrayList and Array

  • The size of an Array is fixed upon creation. Once created, it cannot be changed, whereas the size of an ArrayList is dynamic: it can grow as needed
  • Arrays use less memory than ArrayLists
  • Arrays can handle both primitive and Object types
  • Arrays can be multidimensional - ArrayLists can only have one dimension
  • ArrayLists are Generic. You use the diamond operators (<>) to tell the ArrayList what its data type will be
  • ArrayLists can only handle Object types (because they're Generic)
  • ArrayLists have added functionality