🎓 A1Learner
🔍
0 of 40 lessons completed

Java ArrayList in Depth

An ArrayList stores its items in one ordered row with numbered positions. This lesson goes below the surface, to index operations that shift, remove, find, and view items.

Adding at an index

add with an index places the new item at that position. The printed list shows the items that moved aside.

import java.util.ArrayList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<String> names = new ArrayList<>();
    names.add("Ana");
    names.add("Ben");
    names.add("Cid");
    names.add(1, "Dan");
    System.out.println(names);
  }
}

The two remove forms

The first list loses the item at index 2, which is 30. The second list loses the value 2 itself.

import java.util.ArrayList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<Integer> values = new ArrayList<>();
    values.add(10);
    values.add(20);
    values.add(30);
    values.remove(2);
    System.out.println(values);
    List<Integer> other = new ArrayList<>();
    other.add(10);
    other.add(2);
    other.add(30);
    other.remove(Integer.valueOf(2));
    System.out.println(other);
  }
}

Finding an item

indexOf and contains read the list without changing it. A missing item gives index -1 and the answer false.

import java.util.ArrayList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<String> fruits = new ArrayList<>();
    fruits.add("fig");
    fruits.add("pear");
    fruits.add("plum");
    System.out.println(fruits.indexOf("pear"));
    System.out.println(fruits.contains("plum"));
    System.out.println(fruits.indexOf("kiwi"));
    System.out.println(fruits.contains("kiwi"));
  }
}

A sublist is a view

subList(1, 3) views the middle two items of the list. Setting an item through the view writes into the original list.

import java.util.ArrayList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<String> letters = new ArrayList<>();
    letters.add("a");
    letters.add("b");
    letters.add("c");
    letters.add("d");
    List<String> view = letters.subList(1, 3);
    view.set(0, "B");
    System.out.println(view);
    System.out.println(letters);
  }
}

Example

import java.util.ArrayList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<String> colours = new ArrayList<>();
    colours.add("red");
    colours.add("green");
    colours.add("blue");
    colours.add(1, "yellow");
    colours.remove("green");
    System.out.println(colours);
    System.out.println(colours.size());
    System.out.println(colours.indexOf("blue"));
  }
}

Key points

  • Adding at an index shifts every later item one position to the right.
  • remove with an index removes a position, and remove with an object removes a value.
  • indexOf reports the first matching position, and contains answers true or false.
  • subList returns a view, so a change through the view changes the original list.

In this example, yellow is inserted at index 1, green is removed by value, and the final list holds three colours with blue at index 2.

Note

On a list of Integer values the two remove forms look alike, so the argument type decides.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

On a List<Integer>, what does remove(2) remove?