Saturday, January 11, 2020

ArrayList

ArrayList

ArrayList is part of Java Collection. Lists may contain duplicate elements. The Java platform contains two general-purpose List implementations. ArrayList, which is usually the better-performing implementation, and LinkedList which offers better performance under certain circumstances. ArrayList and LinkedList are very powerful tools to solve several programming problems easily. The ArrayList obtains the behavior of both an array and a List. It means it is accessible by indices and it is dynamic in nature( we can add or remove elements from the list anytime).

ArrayList in Java Collection
Collection Class Hierarchy
For example
timport java.util.ArrayList;
import java.util.Iterator;

public class Test {

      public static void main(String[] args) {
            ArrayList<String> colors=new ArrayList<>();
            //Generics are type parameters which are provided between < >
            //JVM can now identify the type of collection
            colors.add("RED");
            colors.add("BLUE");
            colors.add("GREEN");
            colors.add("ORANGE");
            colors.add("PURPLE");
            colors.remove(1);//remove element from index
            colors.add(2, "CYAN");//add element at input index
            /*for(String s:colors)//accessible by for loop
                  System.out.println(s);
            */
            //Accessible by iterator()
            Iterator<String> i=colors.iterator();
            while(i.hasNext())
            {
                  System.out.println(i.next());
            }
            //size of the array
            System.out.println("Size of Arraylist="+colors.size());
      }
      }
Output:
RED
GREEN
CYAN
ORANGE
PURPLE
Size of Arraylist=5

ArrayList Methods 

Several methods to operate on the ArrayList in Java are available. Few of them are listed.

add()

To add a new Object in the List.

addAll()

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

clear()

Clears all the list

size()

Returns the size of ArrayList.

clone()

Return a cloned LinkedList Object

contains()

Return a boolean value on the basis of if the collection contains the input Object or not.

containsAll()
Return a boolean value on the basis of if the collection contains the input Collection or not.

get(index)

Returns the Object at the input index.

isEmpty()

Return the boolean value if the list is empty or not.

iterator()

Returns an iterator over the elements in this list in the proper sequence.

 
sort()

Sorts this list according to the order induced by the specified by the Comparator.

trimToSize()

Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.

indexOf()

Returns the index of the input object.

lastIndexOf()

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

remove(Object)

Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

remove(index)

Removes the element from the input index.

Video Tutorial