How to define custom ArrayList in java

Nowadays this is also a popular interview question. Let’s see how we can implement custom ArrayList. Here we are going to define only add method, which will add the element to list.

Step 1 – Define a class CustomArrayListExample  by extending AbstractList.

public class CustomArrayListExample extends AbstractList {

}

 

Step 2 – Define the following instance variable.

public class CustomArrayListExample extends AbstractList {
	Object[] elementData;
	private int size;
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
	private static final int DEFAULT_CAPACITY = 10;
	protected int modCount = 0;


}

Step 3 -Define default constructor and initialize the elementData array.

public class CustomArrayListExample extends AbstractList {
	Object[] elementData;
	private int size;
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
	private static final int DEFAULT_CAPACITY = 10;
	protected int modCount = 0;

	CustomArrayListExample() {
		elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
	}


}

 

Step 4 – Define ensureCapacity() which will increase the capacity of the list once it will reach threshold capacity and write below logic.

package customarraylist;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;

public class CustomArrayListExample extends AbstractList {
	Object[] elementData;
	private int size;
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
	private static final int DEFAULT_CAPACITY = 10;
	protected int modCount = 0;

	CustomArrayListExample() {
		elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
	}


	private void ensureCapacity(int minCapacity) {
		if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
			minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
		}
		
		
		if (minCapacity - elementData.length > 0) {
			int oldCapacity = elementData.length;
			int newCapacity = oldCapacity + (oldCapacity >> 1);
			if (newCapacity - minCapacity < 0) {
				newCapacity = minCapacity;
			}

			elementData = Arrays.copyOf(elementData, newCapacity);
		}
		
		
	}

	
}

 

Step 5 – Define add() method and call ensureCapacity() method.

package customarraylist;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;

public class CustomArrayListExample extends AbstractList {
	Object[] elementData;
	private int size;
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
	private static final int DEFAULT_CAPACITY = 10;
	protected int modCount = 0;

	CustomArrayListExample() {
		elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
	}


	private void ensureCapacity(int minCapacity) {
		if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
			minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
		}
		
		
		if (minCapacity - elementData.length > 0) {
			int oldCapacity = elementData.length;
			int newCapacity = oldCapacity + (oldCapacity >> 1);
			if (newCapacity - minCapacity < 0) {
				newCapacity = minCapacity;
			}

			elementData = Arrays.copyOf(elementData, newCapacity);
		}
		
		
	}

	@Override
	public boolean add(Object e) {
		ensureCapacity(size + 1); // Increments modCount!!
		elementData[size++] = e;
		return true;
	}

	
}

 

We have add() method which will add elements, we have initial capacity 10 as soon as we will add the 10th element new capacity will become 15.


Let’s define get() method and the main method. Inside the main method create an object of CustomArrayListExample and add some elements. Iterate the elements using the iterator and for each loop.

 

 

package customarraylist;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;

public class CustomArrayListExample extends AbstractList {
	Object[] elementData;
	private int size;
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
	private static final int DEFAULT_CAPACITY = 10;
	protected int modCount = 0;

	CustomArrayListExample() {
		elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
	}


	private void ensureCapacity(int minCapacity) {
		if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
			minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
		}
		
		
		if (minCapacity - elementData.length > 0) {
			int oldCapacity = elementData.length;
			int newCapacity = oldCapacity + (oldCapacity >> 1);
			if (newCapacity - minCapacity < 0) {
				newCapacity = minCapacity;
			}

			elementData = Arrays.copyOf(elementData, newCapacity);
		}
		
		
	}

	@Override
	public boolean add(Object e) {
		ensureCapacity(size + 1); // Increments modCount!!
		elementData[size++] = e;
		return true;
	}

	public static void main(String[] args) {
		CustomArrayListExample arrayListExample = new CustomArrayListExample();
		arrayListExample.add("ram");
		arrayListExample.add("mohan");
		arrayListExample.add("sohan");

		

		System.out.println("just print the list elements---");
		System.out.println(arrayListExample);

		Iterator it = arrayListExample.iterator();
		System.out.println("Using iterator---");
		while (it.hasNext()) {
			System.out.println(it.next());
		}

		System.out.println("Using for-each loop---");
		for (Object str : arrayListExample) {
			System.out.println(str);
		}

	}

	@Override
	public Object get(int index) {
		rangeCheck(index);
		return elementData[index];
	}

	public void rangeCheck(int index) {
		if (index >= size) {
			throw new IndexOutOfBoundsException();
		}
	}

	@Override
	public int size() {

		return size;
	}
}

Output is :

just print the list elements—
[ram, mohan, sohan]
Using iterator—
ram
mohan
sohan
Using for-each loop—
ram
mohan
sohan


 

For more details see how ArrayList works in java.