Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Array list

Java ArrayList

The ArrayList class, located in the java.util package, is a dynamically resizable array.

In Java, the distinction between a built-in array and an ArrayList lies in their modifiability: arrays have a fixed size, necessitating the creation of a new array to add or remove elements, whereas an ArrayList allows for dynamic addition and removal of elements. Additionally, their syntax differs slightly.

Example

Instantiate an ArrayList object named “cars” designed to store strings:

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

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

If you’re unsure about the concept of a package, you can refer to our Java Packages Tutorial for clarification.

Add Items

The ArrayList class provides numerous helpful methods. For instance, to append elements to the ArrayList, utilize the add() method:

Example

import java.util.ArrayList;
 public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    System.out.println(cars);
  }
}

Access an Item

To access an element in the ArrayList, use the get() method and refer to the index number:

Example

cars.get(0);
Keep in mind: Array indexes commence with 0, where [0] denotes the first element, [1] denotes the second element, and so forth.

Change an Item

To alter an element, employ the set() method and specify the index number:

Example

cars.set(0, "Opel");

Remove an Item

To eliminate an element, utilize the remove() method and specify the index number:

Example

cars.remove(0);

To eliminate all elements within the ArrayList, employ the clear() method:

Example

cars.clear();

ArrayList Size

To determine the number of elements in an ArrayList, utilize the size() method.

Example

cars.size();

Loop Through an ArrayList

Iterate through the elements of an ArrayList using a for loop, specifying the number of iterations based on the size() method:

Example

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    for (int i = 0; i < cars.size(); i++) {
      System.out.println(cars.get(i));
    }
  }
}

Another way to iterate through an ArrayList is by using the for-each loop.

Example

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    for (String i : cars) {
      System.out.println(i);
    }
  }
}

Other Types

Elements stored in an ArrayList are objects. In the previous examples, we instantiated elements (objects) of type “String”. It’s important to note that in Java, a String is an object, not a primitive type. When using other types such as int, you need to specify the equivalent wrapper class, for example, Integer. For other primitive types, use Boolean for boolean, Character for char, Double for double, and so forth

Example

Instantiate an ArrayList to store numbers, adding elements of type Integer.

import java.util.ArrayList;
 public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(10);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(25);
    for (int i : myNumbers) {
      System.out.println(i);
    }
  }
}

Sort an ArrayList

Another valuable class within the java.util package is the Collections class, which contains the sort() method for arranging lists alphabetically or numerically.

Example

Arrange an ArrayList of Strings in sorted order.

 import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class
 public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    Collections.sort(cars);  // Sort cars
    for (String i : cars) {
      System.out.println(i);
    }
  }
}

Example

Arrange an ArrayList of Integers in sorted order.

 import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class
 public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(33);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(34);
    myNumbers.add(8);
    myNumbers.add(12);
     Collections.sort(myNumbers);  // Sort myNumbers
     for (int i : myNumbers) {
      System.out.println(i);
    }
  }
}