Custom List Implementation Example in Java

In this tutorial, I am giving an example implementation of List in java. Feel free to customize the behavior of list, by adding or removing methods from DemoList class. If you have some ideas to make this implementation better, please share with us.

Java List Implementation Example

In this class, we are creating a List implementation with following features:

  • List may grow from zero to infinite size (at least theoretically).
  • List will be initialized with minimum 10 elements at the time of creation.
  • List will provide methods for fetching, adding, removing and printing the list at any state in its lifecycle.

Sourcecode of List Implementation

package com.howtodoinjava.datastructure;

import java.util.Arrays;

public class DataList<E> 
	//Size of list
	private int size = 0;
	//Default capacity of list is 10
	private static final int DEFAULT_CAPACITY = 10;
	//This array will store all elements added to list
	private Object elements[];

	//Default constructor
	public DataList() {
		elements = new Object[DEFAULT_CAPACITY];

	//Add method
	public void add(E e) {
		if (size == elements.length) {
		elements[size++] = e;
	//Get method
	public E get(int i) {
		if (i >= size || i < 0) {
			throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
		return (E) elements[i];
	//Remove method
	public E remove(int i) {
		if (i >= size || i < 0) {
			throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
		Object item = elements[i];
		int numElts = elements.length - ( i + 1 ) ;
		System.arraycopy( elements, i + 1, elements, i, numElts ) ;
		return (E) item;
	//Get Size of list
	public int size() {
		return size;
	//Print method
	public String toString() 
		 StringBuilder sb = new StringBuilder();
		 for(int i = 0; i < size ;i++) {
		 return sb.toString();
	private void ensureCapacity() {
		int newSize = elements.length * 2;
		elements = Arrays.copyOf(elements, newSize);

Let’s quickly test out our List implementation.

package com.howtodoinjava.datastructure;

public class Main
	public static void main(String[] args) 
		DataList<Integer> list = new DataList<>();

		//Add elements
		//Remove elements from index
		//Get element with index
		System.out.println( list.get(0) );
		System.out.println( list.get(1) );

		//List Size

As verified in above output, our list implementation is capable of providing all necessary features.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Custom List Implementation Example in Java”

  1. Hi, I have a suggestion on the remove method mentioned here. Its an alternative which might (may be marginally) make the program bit faster.

    the line which calculates the number of remaining elements after removal, i.e.

    int numElts = elements.length - ( i + 1 ) ;

    could also be written as –

    int numElts = size - ( i + 1 ) ;

    This will reduce the work of copy of array cells which are not populated. As far as I think.


Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.