Java ArrayList Tutorial with Examples


Java programming language provides the ArrayList which is a class of resizeable array. ArrayList is an implementation of the array structure in programming languages. ArrayList contains different types of elements in a single variable.

ArrayList Features

ArrayList provides some features to be used different implementations.

  • ArrayList inherits the AbstractList class and implements the List interface.
  • ArrayList size can change with the addition and removal of the elements.
  • ArrayList items can be accessed randomly.
  • Java ArrayList provides very similar features to the C++ Vector structure.

Create ArrayList

The ArrayList class is provided by the java.util package. So in order to create ArrayList the java.util.ArrayList class should be imported. During the definition of the ArrayList, the elements or items type should be provided with the ArraList<TYPE> syntax where TYPE is the element or item type. In the following example, we will create an ArrayList which items are String type.

// import the ArrayList class
import java.util.ArrayList; 

// Create an ArrayList object
ArrayList<String> names= new ArrayList<String>(); 

names.add("Ismail");
names.add("Ali");

In the following example we will create an Integer ArrayList where we will also add integers to this ArrayList named numbers.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<Integer> numbers= new ArrayList<Integer>();
    names.add(5);
    names.add(1);
    names.add(-8);
  } 
}

Add Items To The ArrayList

Items can be added into the ArrayList byusing the add() function. The element we want to add will be provided as parameter to the add() function. Keep in mind that the parameter type should be the same with the ArrayList we want to add. In the following example we will add some new String type items into the ArrayList named names.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");
    System.out.println(names);
  } 
}

Access ArrayList Item

ArrayList provides indexed positioning of the items. This means every item has an index number which starts from 0 by default. We can use the index number in order to access or get an item. In the following examples, we will get the first and second item from the ArrayList names. We will use the get()function to return given index numbered item.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");
System.out.println(names.get(0));
System.out.println(names.get(0)); } }

Change ArrayList Item

Existing ArrayList items can be changed by using the set() function. The set() function will accept two parameters where first one is the index number of the item we want to change and second is the value we want to set. In the following example we will set the 3rd item which index number is 2 value as Ecrin.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");
names.set(2,"Ecrin"); } }

Remove An Item From ArrayList

Items can be removed from the ArrayList where we will use the remove() function. We will also provide the index number of the item we want to remove.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//This will remove the item "Ismail" names.remove(0); } }

Alternatively if we want to remove all items, removing them with the remove() item one by one is a trivial task. We can remove all items by using the clear() function without a parameter.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//This will remove all items names.clear(); } }

Get ArrayList Size

ArrayList provides dynamic storage where the size of the list can change during its lifetime. We can get the size of the ArrayList by using the size() method. But keep in mind that the size provide the count of the items not the index.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//This will return the size of the names ArrayList int name_size = names.size(); } }

Loop or Iterate Over ArrayList

ArrayList items can be iterated with different loop or iteration structures like for, while, etc. Below we will iterate over the ArrayList named names and print each item into the console.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//Loop over the array list items for (int i =0; i< names.size(); i++){

//Print given item to the console
System.out.println(names.get(i));
} } }

ArrayList is an iterable or enumerable object type which means it can be loop like a foreach structure by iteration with the items. For each step, the iterated value will be set to the variable name.

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//Loop over the array list items with for for (String name : names){

//Print given item to the console
System.out.println(name);
} } }

Sort ArrayList Items

By default, ArrayList items are not stored in a sorted manner. All items are stored according to the adding precedence. The Collections class provides the sort() function which can be used to sort given ArrayList. In the following example, we will sort the items in the ArrayList names.

import java.util.ArrayList;
import java.util.Collections; public class MyClass { public static void main(String[] args) { ArrayList<String> names= new ArrayList<String>(); names.add("Ismail"); names.add("Ali"); names.add("Ahmet"); names.add("Elif");

Collections.sort(names); } }

In the following example, we will sort the items that number.

import java.util.ArrayList;
import java.util.Collections; public class MyClass { public static void main(String[] args) { ArrayList<Integer> numbers= new ArrayList<Integer>(); numbers.add(7); numbers.add(1); numbers.add(4); numbers.add(2);

Collections.sort(numbers); } }

LEARN MORE  C String Variable Type

Leave a Comment