How to implement ArrayList with Array in Java

Filed Under: Java

ArrayList is the most popular implementation of List in java.

ArrayList is very similar Array but provides the feature of dynamic space allocation when the number of objects in the list grows. In Object Array we need to provide the size at the time of initialization but that is not required for ArrayList. Actually, when you initialize ArrayList, it automatically assigns its capacity to 10.

Implement ArrayList using Array

ArrayList is implemented on top of array. So here I am trying to implement custom ArrayList with Object Array and provide basic functions such as get(index), add(object) and remove(index).


public class MyArrayList {

	private static final int SIZE_FACTOR=5;
	
	private Object data[];
	
	private int index;
	
	private int size;
	
	public MyArrayList(){
		this.data=new Object[SIZE_FACTOR];
		this.size=SIZE_FACTOR;
	}
	
	public void add(Object obj){
		System.out.println("index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
		if(this.index==this.size-1){
			//we need to increase the size of data[]
			increaseSizeAndReallocate();
		}
		data[this.index]=obj;
		this.index++;
		
	}
	
	private void increaseSizeAndReallocate() {
		this.size=this.size+SIZE_FACTOR;
		Object newData[]=new Object[this.size];
		for(int i=0; i<data.length;i++){
			newData[i]=data[i];
		}
		this.data=newData;
		System.out.println("***index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
	}
	
	public Object get(int i) throws Exception{
		if(i>this.index-1){
			throw new Exception("ArrayIndexOutOfBound");
		}
		if(i<0){
			throw new Exception("Negative Value");
		}
		return this.data[i];
		
	}
	
	public void remove(int i) throws Exception{
		if(i>this.index-1){
			throw new Exception("ArrayIndexOutOfBound");
		}
		if(i<0){
			throw new Exception("Negative Value");
		}
		System.out.println("Object getting removed:"+this.data[i]);
		for(int x=i; x<this.data.length-1;x++){
			data[x]=data[x+1];
		}
		this.index--;
	}

	public static void main(String[] args) throws Exception {
		MyArrayList mal = new MyArrayList();
		mal.add("0");
		mal.add("1");
		mal.add("2");
		mal.add("3");
		mal.add("4");
		mal.add("5");
		mal.add("6");
		mal.add("7");
		mal.add("8");
		mal.add("9");
		
		mal.remove(5);
		System.out.println(mal.get(7));
	}

}

This is just the basic implementation of ArrayList using Array and to understand how it’s implemented. For development purpose, use the ArrayList provided with JDK.

java custom arraylist, arraylist implementation using array in java

Below is the output produced when we execute the above program.


pankaj:Downloads pankaj$ javac MyArrayList.java 
pankaj:Downloads pankaj$ java MyArrayList
index:0size:5data size:5
index:1size:5data size:5
index:2size:5data size:5
index:3size:5data size:5
index:4size:5data size:5
***index:4size:10data size:10
index:5size:10data size:10
index:6size:10data size:10
index:7size:10data size:10
index:8size:10data size:10
index:9size:10data size:10
***index:9size:15data size:15
Object getting removed:5
8
pankaj:Downloads pankaj$ 
You can check out more java code examples from our GitHub Repository.

References:

Comments

  1. Samba siva says:

    Nice example.

  2. Vishakha says:

    In the above code, I think the line no. 7 has syntax error
    currently it is as shown : private Object data[];
    should not it be : private Object[] data;

    1. Akshay says:

      Both are same, so it doesn’t throw syntax error

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages