Fundamentals 8 min read

Understanding Java List Implementations: ArrayList, LinkedList, Vector, and Their Operations

This article explains the principles behind Java's mutable array implementations, details the characteristics and use‑cases of List, ArrayList, LinkedList, and Vector, and provides code examples for common operations such as adding, removing, setting, retrieving elements, and using iterators.

Java Captain
Java Captain
Java Captain
Understanding Java List Implementations: ArrayList, LinkedList, Vector, and Their Operations

In Java, mutable arrays work by repeatedly creating new arrays and copying the old contents into the new one; the following explains the usage of Java List.

List: elements are ordered (retrieved in the same order they are stored) and can contain duplicates because the collection has an index.

ArrayList: underlying data structure is an array (its length grows by about 50% when expanded); fast random access but slower insert/delete; not synchronized.

LinkedList: underlying data structure is a linked list; slower random access but faster insert/delete.

Vector: also uses an array internally but is synchronized; its length grows by 100% when expanded; both access and modification are slower, and it has largely been replaced by ArrayList.

List‑specific methods are those that operate on indices, which are unique to this collection hierarchy.

boolean add(int index, E element)
boolean addAll(int index, Collection
c)
public static void List_add(){
    ArrayList a1 = new ArrayList();
    a1.add("java");
    a1.add("php"); // List elements can repeat
    a1.add(".net");
    System.out.println("Original collection: " + a1);
    a1.add(1, "Flash");
    a1.add(0, "ps");
    System.out.println(a1);

    ArrayList a2 = new ArrayList();
    a2.add("javascript");
    a2.add("3dMax");
    a2.add("IBM");

    a1.addAll(0, a2);
    System.out.println(a1);
}

Removing an element at a specific position:

boolean remove(int index)
public static void List_remove(){
    ArrayList a1 = new ArrayList();
    a1.add("javascript");
    a1.add("php");
    a1.add("flash");
    System.out.println("Original collection: " + a1);

    a1.remove(0);
    System.out.println(a1);
}

Modifying an element at a given index (set returns the replaced element):

public static void List_set() {
    ArrayList a1 = new ArrayList();
    a1.add("javascript");
    a1.add("php");
    a1.add(".net");
    System.out.println("Original collection: " + a1);

    a1.set(1, "falsh");
    System.out.println(a1);
}

Retrieval methods:

get(int index)   // returns the element at the specified position
subList(int fromIndex, int toIndex) // returns a view of the list between fromIndex (inclusive) and toIndex (exclusive)
public static void List_get() {
    ArrayList a1 = new ArrayList();
    a1.add("java");
    a1.add("php");
    a1.add("flash");

    System.out.println(a1.get(0)); // get element at index 0
    System.out.println(a1.subList(1, 3)); // get sub‑list from index 1 to 2
}

List has a special iterator: ListIterator, which extends Iterator and provides additional operations.

Note: During iteration you must not modify the collection directly (e.g., using add or remove on the list) because it will cause a ConcurrentModificationException. Only the iterator’s methods should be used; for full add, set, or remove capabilities you need ListIterator.

public class ListIteratorDemo {
    public static void main(String[] args) {
        ArrayList a1 = new ArrayList();
        a1.add("java01");
        a1.add("java02");
        a1.add("java03");
        a1.add("java04");
        System.out.println("Original collection: " + a1);

        // Only ListIterator supports add, set, and remove during iteration
        ListIterator li = a1.listIterator();
        while (li.hasNext()) {
            if (li.next().equals("java02")) {
                // li.add("java009"); // can add
                li.set("java006"); // replace current element
            }
        }
    }
}

Vector’s enumeration (similar to an iterator) has been largely superseded by iterators.

public class VectorDemo {
    public static void main(String[] args) {
        Vector v = new Vector();
        v.add("java01");
        v.add("java02");
        v.add("java03");
        v.add("java04");
        for (Enumeration en = v.elements(); en.hasMoreElements(); ) {
            System.out.println(en.nextElement());
        }
    }
}

LinkedList specific methods:

addFirst() – insert element at the head

addLast() – insert element at the tail

getFirst(), getLast() – retrieve but do not remove; throw NoSuchElementException if empty

removeFirst(), removeLast() – retrieve and remove; throw NoSuchElementException if empty

offerFirst(), offerLast() – JDK 1.6 alternatives for insertion

peekFirst(), peekLast() – retrieve without removal, return null if empty

pollFirst(), pollLast() – retrieve with removal, return null if empty

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList link = new LinkedList();
        link.add("java01");
        link.add("java02");
        link.add("java03");
        link.add("java04");
        while (!link.isEmpty()) {
            System.out.println(link.removeLast());
        }
    }
}

Follow the QR code to subscribe to the WeChat public account for daily Java tips.

JavaCollectionslistArrayListIteratorVectorLinkedList
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.