Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

List.add — adding an element to a list

`List.add(E)` appends one element to a list and returns `true` on success. The indexed overload inserts an element at a position you choose.

Version: Java SE 21

Syntax

boolean add(E e)
void add(int index, E element)

In Java SE 21, the java.util.List<E> interface has two List.add overloads. The one-argument method, List.add(E), appends an element to the existing list. E is the element type, such as String in List<String>. The method preserves the order of the existing elements and returns true on success, not an index or a new list. ArrayList accepts duplicates and null values.

The overload add(int index, E element) inserts at a zero-based position and returns void. The element at that position and any later elements shift one place to the right. index == size() appends; an index outside 0 <= index && index <= size() is invalid and causes IndexOutOfBoundsException when insertion is supported. Unlike set(index, element), insertion does not replace an element.

Adding is optional: the type List<E> alone does not guarantee that a particular list accepts additions. An unsupported call throws UnsupportedOperationException rather than returning false. A list that supports adding can still reject null with NullPointerException, a disallowed type with ClassCastException, or a disallowed element property with IllegalArgumentException.

List.of(...) creates an unmodifiable list. Arrays.asList(...) returns an array-backed list that allows replacement but has a fixed size. Neither supports add. If you need a separate list that can grow, use new ArrayList<>(source). Appending to the copy does not change the source list.

Examples

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> source = List.of("alpha", "beta");
        List<String> tags = new ArrayList<>(source);

        boolean added = tags.add("alpha");
        tags.add(1, "gamma");

        System.out.println(added);
        System.out.println(tags);
        System.out.println(source);

        try {
            source.add("delta");
        } catch (UnsupportedOperationException e) {
            System.out.println("UnsupportedOperationException");
        }
    }
}

Result

true
[alpha, gamma, beta, alpha]
[alpha, beta]
UnsupportedOperationException

Appending a second `alpha` to `tags` returns `true`. Inserting `gamma` at index `1` shifts `beta` and the second `alpha` to the right. `source` remains unchanged and rejects the attempt to add `delta`.

Related lessons

  • Java · Lesson 1 · Advanced · Version: 2
  • Java · Lesson 2 · Advanced · Version: 2
  • Java · Lesson 6 · Advanced · Version: 2
  • Java · Lesson 8 · Advanced · Version: 2
  • Java · Lesson 10 · Advanced · Version: 2
  • Java · Lesson 13 · Advanced · Version: 2
  • Java · Lesson 21 · Advanced · Version: 2
  • Java · Lesson 33 · Advanced · Version: 2
  • Java · Lesson 35 · Advanced · Version: 2
  • Java · Lesson 36 · Advanced · Version: 2
  • Java · Lesson 38 · Advanced · Version: 2
  • Java · Lesson 19 · Basic · Version: 2
  • Java · Lesson 29 · Basic · Version: 2
  • Java · Lesson 39 · Basic · Version: 2
  • Java · Lesson 44 · Basic · Version: 2
  • Java · Lesson 54 · Basic · Version: 2
  • Java · Lesson 57 · Basic · Version: 2
  • Java · Lesson 59 · Basic · Version: 2
  • Java · Lesson 62 · Basic · Version: 2
  • Java · Lesson 2 · Intermediate · Version: 2
  • Java · Lesson 5 · Intermediate · Version: 2
  • Java · Lesson 6 · Intermediate · Version: 2
  • Java · Lesson 33 · Intermediate · Version: 2
  • Java · Lesson 34 · Intermediate · Version: 2
  • Java · Lesson 36 · Intermediate · Version: 2
  • Java · Lesson 39 · Intermediate · Version: 2
  • Java · Lesson 50 · Intermediate · Version: 2

Sources