0
Sponsored Links


Ad by Google
This question is again a very popular interview question in Banking and Telecom domain, sometimes interviewer ask you to create your own ArrayList and provide the implementation of few methods such as add/get/remove/size/isEmpty etc. The main purpose of this question is to check your internal working knowledge over the ArrayList and How much you are aware about the internal implementation of the ArrayList.

Answer of this question is required a good working knowledge of ArrayList, Before writing a program lets see what are the things to keep in mind while creating own ArrayList in java. Well ArrayList is a Resizable-array of an Objects. In Array we don't have the capability of dynamically increasing the size of the array but in ArrayList we can, We know that ArrayList has initial capacity of the element to be added in a list, so first things we need is default initial capacity. second things we need an Array of Objects to store the element of the ArrayList lets start writing our own ArrayList called CustomArrayList with few methods.

Define default initial capacity of the CustomArrayList with below line of code:
private static final int DEFAULT_INITIAL_CAPACITY = 5;
 
Now declare an empty CustomArrayList which will return while creating empty CustomArrayList
private static final Object[] EMPTY_ELEMENT_DATA = {}; 
 
Define the size for the CustomeArrayList
private int size; 
 
Now we need to define an Array of an Object to store the elements
private transient Object[] customArrayListElementData;
 
Ok now lets see the complete program of our CustomArrayList with implementation of size(),isEmpty(),add(),get(),remove(),clear() methods.
public class CustomArrayList<E>{
    private static final int DEFAULT_INITIAL_CAPACITY = 5;
    private static final Object[] EMPTY_ELEMENT_DATA = {};
    private int size;

    /**
     * The array elements to be stored inside 
     * customArrayListElementData.
     */
    private transient Object[] customArrayListElementData;

    /**
     * Constructs a custom arrayList with an initial capacity.
     * @param initialCapacity
     */
    public CustomArrayList(int initialCapacity){
      super();
         if (initialCapacity < 0)
             throw new IllegalArgumentException("Illegal Capacity: "+
                                                initialCapacity);
         this.customArrayListElementData = new Object[initialCapacity];
    }
    
    /**
     * Constructs an empty list.
     */
    public CustomArrayList(){
       super();
          this.customArrayListElementData = EMPTY_ELEMENT_DATA;
    }
    
    /**
     * @return the size of the CustomArrayList
     */
    public int size() {
     return size;
    }

 /**
  * @return true/false if size is greater then 0 return true else false.
  */
 public boolean isEmpty() {
  return size==0;
 }

 /**
  * return true 
  * @param e
  */
 public boolean add(E e) {
   ensureCapacity(size + 1);  
   customArrayListElementData[size++] = e;
   return true;
 }

 public void clear() {
        for (int i = 0; i < size; i++)
            customArrayListElementData[i] = null;

        size = 0;
  
 }
 /**
  * Returns the element at the specified position in this list.
  * @param index
  * @return
  */
 @SuppressWarnings("unchecked")
 public E get(int index) {
    if (index >= size){
     throw new ArrayIndexOutOfBoundsException("array index out of bound exception with index at"+index);
    }
  return (E)customArrayListElementData[index];
 }
 
 /**
  * add element at specific index position and shift the
  * customArrayListElementData.
  * @param index
  * @param element
  */
 public void add(int index, E element) {
  ensureCapacity(size + 1); 
        System.arraycopy(customArrayListElementData, index, customArrayListElementData, index + 1,size - index);
        customArrayListElementData[index] = element;
        size++;
  
 }

 /**
  * Remove the element from the customArrayListElementData
  * and shift the elements position.
  * @param index
  * @return
  */
 @SuppressWarnings("unchecked")
 public E remove(int index) {
  E oldValue = (E)customArrayListElementData[index];

        int removeNumber = size - index - 1;
        if (removeNumber > 0){
            System.arraycopy(customArrayListElementData, index+1, customArrayListElementData, index,removeNumber);
        }
        customArrayListElementData[--size] = null; 
        return oldValue;
 }

    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    private void growCustomArrayList(int minCapacity) {
        int oldCapacity = customArrayListElementData.length;
        int newCapacity = oldCapacity + (oldCapacity /2);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        customArrayListElementData = Arrays.copyOf(customArrayListElementData, newCapacity);
    }
    
    /**
     * ensure the capacity and grow the customArrayList vi 
     * growCustomArrayList(minCapacity);
     * @param minCapacity
     */
    private void ensureCapacity(int minCapacity) {
        if (customArrayListElementData == EMPTY_ELEMENT_DATA) {
            minCapacity = Math.max(DEFAULT_INITIAL_CAPACITY, minCapacity);
        }

        if (minCapacity - customArrayListElementData.length > 0)
            growCustomArrayList(minCapacity);
    }
 /**
  * main method to test the custome array list 
  */ 
 public static void main(String[] args) {
  CustomArrayList<String> strList= new CustomArrayList<>();
  strList.add("str1");
  strList.add("str2");
  System.out.println("after adding elements size ="+strList.size());
  strList.remove(1);
  System.out.println("after removing element size ="+strList.size());
 }
}
 

Out put:
after adding elements size =2
after removing element size =1


Few popular core java interview questions.

Which Collection is better to store 10 millions of record between ArrayList and LinkedList.
What are the Key differences between Comparable and Comparator.
Serializable Vs Externalizable.
Java program to find missing numbers
Why String is Immutable in java


Reference
Sponsored Links

0 comments:

Post a Comment