Tsallaka zuwa abun ciki
Justice Tech School

ArrayList

ArrayList is a resizable list from java.util. Use add, get, set, remove, size, contains and isEmpty. Write the element type in angle brackets, for example ArrayList<String>.

Example
import java.util.ArrayList;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> tools = new ArrayList<>();
    tools.add("Java");
    tools.add("SQL");
    tools.add("Git");
    tools.remove("SQL");
    Collections.sort(tools);
    System.out.println(tools + " size " + tools.size());
    System.out.println(tools.get(0));
    System.out.println(tools.contains("Git"));
  }
}

Result

[Git, Java] size 2
Git
true

Exercise

Add an item to the list.

list.("x");

Quiz Test

Why use ArrayList instead of an array?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.