ArrayList is very similar to Array but provides the feature of dynamic space allocation when the number of objects in the list grows.
In Array, we have 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 initial capacity to 10.
Implement ArrayList using Array
ArrayList is implemented on top of array. Here I am trying to implement custom ArrayList with an Array and provide basic functions such as get(index)
, add(object)
and remove(index)
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
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 the basic implementation of ArrayList using an Array. The idea is to understand how an ArrayList is implemented. For development purposes, use the ArrayList class from the Collections API.
Below is the output produced when we execute the above program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span style="color: #008000;"><code><strong>$ javac MyArrayList.java $ 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 $</strong> </code></span> |
References: