ArrayList in Java 101
Tags: java, algorithms, beginners
Published: 2022-05-28 20:09:15

![IMG](https://images.unsplash.com/photo-1569181801771-7ef61fee284d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80)

A List is an ordered collection, where the user of this interface has control over where in the list each element is going to be inserted. It offers various methods, to add, remove, and look for a specific value in the list.

A List should not be confused as an ArrayList. The first one is a child interface of Collection, and the second one is a class that implements the List interface.

![IMG](https://i.imgur.com/rSFTIfd.png)

So we can implement the idea of the List, to use an ArrayList, it needs to be imported into the program.

```java
//import the list to make it available
import java.util.ArrayList;

public class Program {
    public class void main(String[] args) {
        //main program
    }
}
```

Since all the variables stored in a given list are of the same type, we must define the *Type* in the type of values to be stored.

```java
ArrayList<Type> nameOfTheList = new ArrayList<>();
```

In actual code, an example to create a list for storing *Strings*.

```java
//import the list to make it available
import java.util.ArrayList;

public class Program {
    public class void main(String[] args) {
        //create a String list
        ArrayList<String> list = new ArrayList<>();
    }
}
```

Basically, the first part `ArrayList<String` is the type of the ArrayList, and the command to create is `new ArrayList<>()`.

## Type of values that a List can contain

In Java, we have two categories of variables. The first one, is the primitive data type, which are values like `int` or `double` where they actually hold their values. The second one, is the reference data type, like `ArrayList`, where it contains a reference to the location in memory where is the value related to this variable.

This is why we need to declare the *Type* of the `ArrayList` capitalized. The moment that we create new lists, like the ones below.

```java
ArrayList<Integer> list = new ArrayList<>();
list.add(42);
```

```java
ArrayList<Double> list = new ArrayList<>();
list.add(4.20);
```

```java
ArrayList<Boolean> list = new ArrayList<>();
list.add(true);
```

The `ArrayList` assumes that all variables in it are **reference types**. Java automatically converts an `int` variable into `Integer`. Amazing, right?

So, it is pretty simple to create an `ArrayList`. First we define the `Type`, and later we assign to create using the `new` command.

## Interface

The *List* Interface are zero-based. It means that we start couting by 0. So the first position of an element, is not the number 1, but it is the zero-number. And with the actual knowledge it is easy the way that we can do operations, use methods and retrieve values through their index (position).

To understand this better, let`s keep constructing the program we start creating in the beginning of this article.

```java

//import the list to make it available
import java.util.ArrayList;

public class Program {
    public class void main(String[] args) {
        //create a String list
        ArrayList<String> list = new ArrayList<>();
        
        //add three values
        list.add("First");
        list.add("Second");
        list.add("Third");
        
        //retrieve value through their index (position)
        System.out.println(list.get(0));
    }
}

```

Can you guess which element will be displayed?

You`re totally right if you guessed First.

```java
First
```

## List Methods

Here, we can verify a bunch of useful methods for the `ArrayList`.

- Verify if the ArrayList is empty, returns **true** if the list contains no element.

```java
ArrayList<String> list = new ArrayList<>();
System.out.println(list.isEmpty());
```

- Adding to a list is done with the method below.

```java
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(3);
```

- The size method is straightforward.

```java
ArrayList<String> wordList = new ArrayList<>();
int totalSize = wordList.size();
```

- To retrieve a value from a certain index, we use the *.get* method.

```java
ArrayList<Double> decimals = new ArrayList<>();
decimals.add(0.01);
String decimal = decimals.get(0);
System.out.println(decimal);
```

- Removing elements from a list, it can receives both, a parameter either the value, or the index of the value to be removed.

```java
ArrayList<String> list = new ArrayList<>();
list.remove("hello world!");
list.remove(3);
```

- Verify the existancec of a value with the *contains* method, returns **true** if the list contains the specified element.

```java
ArrayList<String> list = new ArrayList<>();
boolean found = list.contains("hello world!");
```

## Reference

1. [Docs Oracle](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html)
2. [javaTpoint](https://www.javatpoint.com/java-arraylist)
3. [GeeksforGeeks](https://www.geeksforgeeks.org/arraylist-in-java/?ref=lbp)