codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java ArrayList

Java ArrayList

A list that can grow.

ArrayList (from java.util) is a resizable list. Use add, get, remove, size and Collections.sort. The type in angle brackets, like <String>, says what it holds (these are called generics).

Example code

import java.util.*;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> fruits = new ArrayList<>();
    fruits.add("pear");
    fruits.add("apple");
    System.out.println(fruits);
    fruits.add(0, "fig");
    System.out.println(fruits.get(1));
    fruits.remove("apple");
    Collections.sort(fruits);
    System.out.println(fruits);
    System.out.println(fruits.size());
  }
}

Run this code in the online editor

← Java enumsJava HashMap and HashSet →