Java Program to Implement ArrayList API

This Java program is to Implement ArrayList Collection API. Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

Here is the source code of the Java program to Implement ArrayList Collection API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.ListIterator;
  7. import java.util.Set;
  8.  
  9. public class ArrayListImpl<E>
  10. {
  11.     private ArrayList<E> arrayList;
  12.  
  13.     /* Constructs an empty list with an initial capacity of ten. */
  14.     public ArrayListImpl()
  15.     {
  16.         arrayList = new ArrayList<E>();
  17.     }
  18.  
  19.     /*
  20.      * Constructs a list containing the elements of the specified collection, in
  21.      * the order they are returned by the collection's iterator.
  22.      */
  23.     public ArrayListImpl(Collection<? extends E> c)
  24.     {
  25.         arrayList = new ArrayList<E>(c);
  26.     }
  27.  
  28.     /* Constructs an empty list with the specified initial capacity. */
  29.     public ArrayListImpl(int initialCapacity)
  30.     {
  31.         arrayList = new ArrayList<E>(initialCapacity);
  32.     }
  33.  
  34.     /* Appends the specified element to the end of this list. */
  35.     public boolean add(E e)
  36.     {
  37.         return arrayList.add(e);
  38.     }
  39.  
  40.     /* Inserts the specified element at the specified position in this list. */
  41.     public void add(int index, E element)
  42.     {
  43.         arrayList.add(index, element);
  44.     }
  45.  
  46.     /*
  47.      * Appends all of the elements in the specified collection to the end of
  48.      * this list, in the order that they are returned by the specified
  49.      * collection's Iterator.
  50.      */
  51.     public boolean addAll(Collection<? extends E> c)
  52.     {
  53.         return arrayList.addAll(c);
  54.     }
  55.  
  56.     /*
  57.      * inserts all of the elements in the specified collection into this list,
  58.      * starting at the specified position.
  59.      */
  60.     public boolean addAll(int index, Collection<? extends E> c)
  61.     {
  62.         return arrayList.addAll(index, c);
  63.     }
  64.  
  65.     /* Removes all of the elements from this list. */
  66.     public void clear()
  67.     {
  68.         arrayList.clear(); 
  69.     }
  70.  
  71.     /* Returns a shallow copy of this ArrayList instance. */
  72.     public Object clone()
  73.     {
  74.         return arrayList.clone();
  75.     }
  76.  
  77.     /* Returns true if this list contains the specified element. */
  78.     public boolean contains(Object o)
  79.     {
  80.         return arrayList.contains(o);
  81.     }
  82.  
  83.     /*
  84.      * Increases the capacity of this ArrayList instance, if necessary, to
  85.      * ensure that it can hold at least the number of elements specified by the
  86.      * minimum capacity argument.
  87.      */
  88.     public void ensureCapacity(int minCapacity)
  89.     {
  90.         arrayList.ensureCapacity(minCapacity);
  91.     }
  92.  
  93.     /* Returns the element at the specified position in this list. */
  94.     public E get(int index)
  95.     {
  96.         return arrayList.get(index);
  97.     }
  98.  
  99.     /*
  100.      * Returns the index of the first occurrence of the specified element in
  101.      * this list, or -1 if this list does not contain the element.
  102.      */
  103.     public int indexOf(Object o)
  104.     {
  105.         return arrayList.indexOf(o);
  106.     }
  107.  
  108.     /* Returns true if this list contains no elements. */
  109.     public boolean isEmpty()
  110.     {
  111.         return arrayList.isEmpty();
  112.     }
  113.  
  114.     /* Returns an iterator over the elements in this list in proper sequence. */
  115.     public Iterator<E> iterator()
  116.     {
  117.         return arrayList.iterator();
  118.     }
  119.  
  120.     /*
  121.      * Returns the index of the last occurrence of the specified element in this
  122.      * list, or -1 if this list does not contain the element.
  123.      */
  124.     public int lastIndexOf(Object o)
  125.     {
  126.         return arrayList.lastIndexOf(o);
  127.     }
  128.  
  129.     /*
  130.      * Returns a list iterator over the elements in this list (in proper
  131.      * sequence).
  132.      */
  133.     public ListIterator<E> listIterator()
  134.     {
  135.         return arrayList.listIterator();
  136.     }
  137.  
  138.     /*
  139.      * Returns a list iterator over the elements in this list (in proper
  140.      * sequence), starting at the specified position in the list.
  141.      */
  142.     public ListIterator<E> listIterator(int index)
  143.     {
  144.         return arrayList.listIterator(index);
  145.     }
  146.  
  147.     /* Removes the element at the specified position in this list. */
  148.     public E remove(int index)
  149.     {
  150.         return arrayList.remove(index);
  151.     }
  152.  
  153.     /*
  154.      * Removes the first occurrence of the specified element from this list, if
  155.      * it is present.
  156.      */
  157.     public boolean remove(Object o)
  158.     {
  159.         return arrayList.remove(o);
  160.     }
  161.  
  162.     /*
  163.      * Removes from this list all of its elements that are contained in the
  164.      * specified collection.
  165.      */
  166.     public boolean removeAll(Collection<?> c)
  167.     {
  168.         return arrayList.removeAll(c);
  169.     }
  170.  
  171.     /*
  172.      * Retains only the elements in this list that are contained in the
  173.      * specified collection.
  174.      */
  175.     public boolean retainAll(Collection<?> c)
  176.     {
  177.         return arrayList.removeAll(c);
  178.     }
  179.  
  180.     /*
  181.      * Replaces the element at the specified position in this list with the
  182.      * specified element.
  183.      */
  184.     public E set(int index, E element)
  185.     {
  186.         return arrayList.set(index, element);
  187.     }
  188.  
  189.     /* Returns the number of elements in this list. */
  190.     public int size()
  191.     {
  192.         return arrayList.size();
  193.     }
  194.  
  195.     /*
  196.      * Returns a view of the portion of this list between the specified
  197.      * fromIndex, inclusive, and toIndex, exclusive.
  198.      */
  199.     public List<E> subList(int fromIndex, int toIndex)
  200.     {
  201.         return arrayList.subList(fromIndex, toIndex);
  202.     }
  203.  
  204.     /*
  205.      * Returns an array containing all of the elements in this list in proper
  206.      * sequence (from first to last element).
  207.      */
  208.     public Object[] toArray()
  209.     {
  210.         return arrayList.toArray();
  211.     }
  212.  
  213.     /*
  214.      * Returns an array containing all of the elements in this list in proper
  215.      * sequence (from first to last element); the runtime type of the returned
  216.      * array is that of the specified array.
  217.      */
  218.     public <T> T[] toArray(T[] a)
  219.     {
  220.         return arrayList.toArray(a);
  221.     }
  222.  
  223.     /*
  224.      * Trims the capacity of this ArrayList instance to be the list's current
  225.      * size.
  226.      */
  227.     public void trimToSize()
  228.     {
  229.         arrayList.trimToSize();
  230.     }
  231.  
  232.     public static void main(String... arg)
  233.     {
  234.         ArrayListImpl<Integer> arrayList = new ArrayListImpl<Integer>();
  235.  
  236.         arrayList.add(10);
  237.         arrayList.add(20);
  238.         arrayList.add(30);
  239.         arrayList.add(3, 40);
  240.         arrayList.add(-10);
  241.  
  242.         Set<Integer> addAll = new HashSet<Integer>();
  243.         addAll.add(101);
  244.         addAll.add(200);
  245.         addAll.add(300);
  246.         arrayList.addAll(addAll);
  247.  
  248.         Set<Integer> indexAddAll = new HashSet<Integer>();
  249.         indexAddAll.add(101);
  250.         indexAddAll.add(102);
  251.         indexAddAll.add(103);
  252.  
  253.         arrayList.addAll(5, indexAddAll);
  254.  
  255.         if (arrayList.contains(101))
  256.             System.out.println("the arrayList contains 101");
  257.         else
  258.             System.out.println("the arrayList does not contain 101");
  259.  
  260.         arrayList.ensureCapacity(15);
  261.         System.out.println("the arrayList contains " + arrayList.get(5)+ " at index 5");
  262.  
  263.         System.out.println("the index of 101 is " + arrayList.indexOf(101));
  264.         System.out.println("the elements of arrayList are");
  265.         Iterator<Integer> itr = arrayList.iterator();
  266.         while (itr.hasNext())
  267.         {
  268.             System.out.print(itr.next() + "\t");
  269.         }
  270.         System.out.println();
  271.  
  272.         System.out.println("the last index of 101 is "+ arrayList.lastIndexOf(101));
  273.         System.out.println("elements from index 3 are");
  274.         ListIterator<Integer> listIterator = arrayList.listIterator(3);
  275.         while (listIterator.hasNext())
  276.         {
  277.             System.out.print(listIterator.next() + "\t");
  278.         }
  279.         System.out.println();
  280.  
  281.         // removes element at index 5
  282.         arrayList.remove(5);
  283.  
  284.         System.out.println("elements removed and retained");
  285.         Set<Integer> removeAll = new HashSet<Integer>();
  286.         removeAll.add(30);
  287.         removeAll.add(300);
  288.         removeAll.add(101);
  289.  
  290.         arrayList.removeAll(removeAll);
  291.         Set<Integer> retainAll = new HashSet<Integer>();
  292.         retainAll.add(10);
  293.         retainAll.add(20);
  294.         retainAll.add(200);
  295.         retainAll.add(-10);
  296.         retainAll.addAll(addAll);
  297.  
  298.         arrayList.retainAll(retainAll);
  299.         arrayList.set(1, 101);
  300.         System.out.println("the size of the arrayList is " + arrayList.size());
  301.         System.out.println("the elements of the returned list are");
  302.         List<Integer> list = arrayList.subList(0,1);
  303.         for (int i = 0; i < list.size(); i++)
  304.         {
  305.             System.out.println(list.get(i) + "\t");
  306.         }
  307.         System.out.println();
  308.         System.out.println("the elements of returned array are");
  309.         Object[] array = (Object[]) arrayList.toArray();
  310.         for (int i = 0; i < array.length; i++)
  311.         {
  312.             System.out.print(array[i] + "\t");
  313.         }
  314.         System.out.println();
  315.         arrayList.trimToSize();
  316.         System.out.println("the size of the arrayList is " + arrayList.size());
  317.         arrayList.clear();
  318.         if (arrayList.isEmpty())
  319.             System.out.println("the arrayList is empty");
  320.         else
  321.             System.out.println("the arrayList is not empty");
  322.     }
  323. }


$javac ArrayListImpl.java
$java ArrayListImpl
 
the arrayList contains 101
the arrayList contains 102 at index 5
the index of 101 is 7
the elements of arrayList are
10	20	30	40	-10	102	103	101	101	200	300	
the last index of 101 is 8
elements from index 3 are
40	-10	102	103	101	101	200	300	
elements removed and retained
the size of the arrayList is 2
the elements of the returned list are
40	
the elements of returned array are
40	101	
the size of the arrayList is 2
the arrayList is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.