Saturday 28 May 2016

How to implement an ArrayList


Sample Program:-


/**  
  * @author Dixit  
  *  
  */  
 public class CustomArrayListImpl {  
      private static final int LIST_SIZE = 5;  
      private Object data[];  
      private int index;  
      private int size;  
      public CustomArrayListImpl() {  
           this.data = new Object[LIST_SIZE];  
           this.size = LIST_SIZE;  
      }  
      public void add(Object obj) {  
           System.out.println("index:" + this.index + " list size:"  
                     + this.data.length);  
           if (this.index == this.size - 1) {  
                increaseSize();  
           }  
           data[this.index] = obj;  
           this.index++;  
      }  
      private void increaseSize() {  
           this.size = this.size + LIST_SIZE;  
           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 + " List size:"  
                     + this.data.length);  
      }  
      public Object get(int i) throws Exception {  
           if (i > this.index - 1) {  
                throw new Exception("ArrayIndexOutOfBound Occurs");  
           }  
           if (i < 0) {  
                throw new Exception("Negative Value Provided");  
           }  
           return this.data[i];  
      }  
      public void remove(int i) throws Exception {  
           if (i > this.index - 1) {  
                throw new Exception("ArrayIndexOutOfBound Occurs");  
           }  
           if (i < 0) {  
                throw new Exception("Negative Value Provided");  
           }  
           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--;  
      }  
      /**  
       * @param args  
       * @throws Exception  
       */  
      public static void main(String[] args) throws Exception {  
           CustomArrayListImpl list = new CustomArrayListImpl();  
           list.add("0");  
           list.add("1");  
           list.add("2");  
           list.add("3");  
           list.add("4");  
           System.out.println("After resizing :-");  
           list.add("5");  
           list.remove(4);  
           System.out.println(list.get(3));  
      }  
 }  


 Output:  
 index:0 list size:5  
 index:1 list size:5  
 index:2 list size:5  
 index:3 list size:5  
 index:4 list size:5  
 index:4 List size:10  
 After resizing :-  
 index:5 list size:10  
 Object getting removed:4  
 3  

Explanation:-
List initial size is set to 5 and it is increased dynamically every time with more 5 indexes.                        
Limitations:-
In Object array we need to provide the size at the time of initialization which is not required for ArrayList. ArrayList automatically assigns its size to 10.

If List is used in Multithreading environment, where two threads are operating on it. One Thread is iterating over it and another thread is adding element to it, then in that case it will throw ConcurrentModificationException.

In order to avoid ConcurrentModificationException, we need to work on the clone of original ArrayList.


Enjoy Programming:)

No comments:

Post a Comment