o7planning

Java ArrayList Tutorial

View more Tutorials:

1- ArrayList

ArrayList is a class that implements List interface and supports all the features of List including optional ones. Basically an ArrayList manages an array to store its elements, which can be replaced by another array of larger length if the number of ArrayList's elements increases.
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
Here are the characteristics of ArrayList:
  1. ArrayList allows containing duplicate and null elements.
  2. ArrayList maintains the insertion order of the elements.
  3. ArrayList is not synchronized, so it needs to be synchronized to be used in a Multithreading environment.
  4. ArrayList allows random access to elements based on the index.
  5. Operation in ArrayList is a bit slower than LinkedList (See more explanation in operation principle of ArrayList).
ArrayList is quite similar to Vector, except it is asynchronous. In a Multithreading environment it should be wrapped using the Collections.synchronizedList method.
ArrayList<String> arrayList = new ArrayList<String>();
        
List<String> syncList = Collections.synchronizedList(arrayList);
CopyOnWriteArrayList is a thread-safe variant of ArrayList that you might consider using:
ArrayList constructors:
ArrayList()Creates an empty ArrayList object with an internal array with an initial capacity of 10 elements.
ArrayList(Collection<? extends E> c)Creates an ArrayList object containing all the elements of the specified Collection, the order of the elements is determined by the Iterator of the Collection.
ArrayList(int initialCapacity)Creates an empty ArrayList object with an internal array of the specified initial capacity.

2- How does ArrayList store elements?

ArrayList manages an array of objects, all the actions of adding, inserting or removing an element from the ArrayList will produce the action of assigning (or updating) the value to the other elements of the array, it can affects many elements of the array.

arrayList.add(X)

When you add an element to the ArrayList, it will be assigned to an element of the array at the index arrayList.size().

arrayList.add(idx,X)

When you insert an element into the ArrayList, many elements on the array will have to update their values.

arrayList.remove(idx)

Removing an element at index idx from the ArrayList also causes many elements of the array to update with new values.
The ArrayList replaces the array it manages with a new array of a larger length if the number of elements added is greater than the length of the current array.

3- Examples

For example, using ArrayList in a Multithreading environment:
ArrayList_sync.java
package org.o7planning.arraylist.ex;

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

public class ArrayList_sync {

    public static void main(String[] args) throws InterruptedException {
        new ArrayList_sync();
    }
    
    public ArrayList_sync() throws InterruptedException {
         ArrayList<String> arrayList = new ArrayList<String>();
         
         List<String> syncList = Collections.synchronizedList(arrayList);
          
         ThreadA threadA = new ThreadA(syncList);
         ThreadB threadB = new ThreadB(syncList);
         
         threadA.start();
         threadB.start();
         
         threadA.join();
         threadB.join();
         
         for(String s: syncList)  {
             System.out.println(s);
         }
    }
    
   class ThreadA extends Thread {
        private List<String> list;
        public ThreadA(List<String> list) {
            this.list = list;
        }
        @Override
        public void run() {
            for(int i= 0; i< 1000; i++) {
                this.list.add("A "+ i);
            }
        }
    }
    
    class ThreadB extends Thread {
        private List<String> list;
        public ThreadB(List<String> list) {
            this.list = list;
        }
        @Override
        public void run() {
            for(int i= 0; i< 1000; i++) {
                this.list.add("B "+ i);
            }
        }
    }
}
Output:
...
A 938
B 898
A 939
B 899
A 940
B 900
A 941
B 901
A 942
...
ArrayList supports all the features specified in the List interface, including optional features. You can find the best examples of ArrayList in the article below:

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.