# How to use Java ArrayList

**ArrayList** is one of the most used data structures in Java. It allows you to dynamically modify and store a collection of objects. In this tutorial, we introduce you to the syntax of Java ArrayList and explain when to use it.

## What is the difference between ArrayList and Array in Java?

Java ArrayList is dynamic, which means that it grows and shrinks in size when elements are added or removed. It is worth noting that the ArrayList class is part of the **Java Collections Framework** and is not available natively. Unlike arrays, it must be imported from the *java.util* library.

ArrayLists are a suitable choice when the length of a [Java List](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-list/) may potentially vary. Examples include storing objects, **searching or sorting** data, and creating lists or queues.

In contrast, it’s not possible to change the size of an array. This means that you should ideally know the number of objects that the array will hold in advance. Arrays are suitable for managing a predefined set of **primitive data types** such as int, float, char or Boolean.

One drawback of ArrayLists is they can **take longer to access**. While arrays have a fixed continuous block of memory, objects in ArrayLists are not stored contiguously. It is important to take the advantages and disadvantages of the data structures into account so you are able to select the one that works best for your use case.

## What is the syntax for Java ArrayList?

Before creating ArrayList, the corresponding class must be imported from the *java.util* library.

```Java
import java.util.ArrayList;
```

The general syntax is:

```Java
ArrayList<Type> arrayList= new ArrayList<>();
```

‘Type’ stands for the respective data type in Java ArrayList.

The next step is to create lists of `strings` and `integers`.

```Java
ArrayList<String> arrayList= new ArrayList<>();
```

```Java
ArrayList<Integer> arrayList= new ArrayList<>();
```

ArrayLists use the corresponding **wrapper class** of primitive data types so that they are treated like objects. This means you have to specify `integer` instead of `int`.

## Examples of Java ArrayList methods

Operations such as adding or removing elements are not performed on ArrayLists with Java operators, but via **predefined methods**. We’ll show you the most common ArrayList methods below.

### Adding elements

After creating the ArrayList ‘colours’ (`String` type), we’ll add various elements using the `.add()` method.

```Java
import java.util.ArrayList;
class Main {
    public static void main(String[] args){
        ArrayList<String> colours = new ArrayList<>();
        colours.add("blue");
        colours.add("red");
        colours.add("green");
        System.out.println("ArrayList: " + colours);
    }
}
```

This results in:

```Java
ArrayList: [blue, red, green]
```

### Removing elements

To remove objects from Java ArrayList, we use the `.remove()` method and specify the element’s index.

```Java
import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> colours = new ArrayList<>();
        colours.add("blue");
        colours.add("red");
        colours.add("green");
        System.out.println("ArrayList: " + colours);
        String colour = colours.remove(1);
        System.out.println("ArrayList: " + colours);
        System.out.println("Removed Element: " + colour);
    }
}
```

The output shows the modified ArrayList and the element that has been removed:

```Java
ArrayList: [blue, green]
Removed Element: red
```

As with most programming languages, counting in Java starts from 0. This means that the removed element at index 1 is `red`.

### Accessing elements in Java ArrayList

Using the function `.get()`, we can access an element at a specific position.

```Java
import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> clothes = new ArrayList<>();
        clothes.add("jacket");
        clothes.add("shirt");
        clothes.add("pullover");
        System.out.println("ArrayList: " + clothes);
        String str = clothes.get(2);
        System.out.print("Element at index 2: " + str);
    }
}
```

For the output we get:

```Java
ArrayList: [jacket, shirt, pullover]
Element at index 2: pullover
```

### Changing elements

With `.set()`, we can change an element by assigning a new element at a specific index.

```Java
import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> colours = new ArrayList<>();
        colours.add("blue");
        colours.add("red");
        colours.add("green");
        System.out.println("ArrayList: " + colours);
        colours.set(2, "yellow");
        System.out.println("Modified ArrayList: " + colours);
    }
}
```

We now see `yellow` instead of `green` at index 2:

```Java
ArrayList: [blue, red, green]
Modified ArrayList: [blue, red, yellow]
```

### Determining the length of Java ArrayList

The number of elements in ArrayList can easily be calculated using the `.size()` method.

```Java
import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> colours = new ArrayList<>();
        colours.add("blue");
        colours.add("red");
        colours.add("green");
        System.out.println(colours.size());
    }
}
```

This is the result:

```Java
3
```

### Sorting and iterating through ArrayList

To sort Java ArrayList, the collections class must be imported. For the iteration, we use a [Java for-each loop](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-for-each-loop/). For each iteration of the loop, the respective element is output to the console.

```Java
import java.util.ArrayList;
import java.util.Collections;
public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> ages = new ArrayList<Integer>();
        ages.add(20);
        ages.add(54);
        ages.add(17);
        ages.add(9);
        Collections.sort(ages);
        for (int i : ages) {
            System.out.println(i);
        }
    }
}
```

The elements of ArrayList are displayed from smallest to largest:

```Java
9
17
20
54
```


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-arraylist/](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-arraylist/) for AI/LLM consumption.