Arrays vs ArrayLists

The main difference between an array and an arraylist is that arraylists can grow and shrink whereas arrays can't. Arrays have a fixed length so once it is full, more elements cannot be added. If elements are removed from an Arrays, the maximum about of space will not shrink. However, ArrayLists can adjust dynamically in regards to elements. Essentially in Java, ArrayLists can:

  • ArrayList can grow and shrink based on the whether you add / remove elements
  • ArrayList can contain duplicate elements
  • ArrayList maintains the insertion order (elements appear in the same order in which they are inserted)

Methods of ArrayLists

  • addAll(int index, Collection collection)
    • Adds all elements in the above collection to the list; and when a parameter is passed, it adds all the elements of the collection to the end
  • add(int index, element)
    • Adds an element at a specific spot in the list; but when a parameter is passed, it will just add the element at the end
  • clear()
    • Removes all the elements in the list
    • Reference of the list created is still stored.
  • containsAll(Collection collection)
    • Checks if the list contains all the elements in the mentioned collection
  • contains(element)
    • Checks if the list contains the mentioned element
      • Returns true if the element is in the list
  • equals(element)
    • Compares the equality of the mentioned element with other present elements
  • get(int index)
    • Returns elements at the specified index
  • hashCode()
    • Returns the hashcode (an integer value that is associated with each object in Java language) value of the list
  • indexOf(element)
    • Returns the first-placed of the mentioned element
      • Returns -1 if the element isn't prevalent in the list
  • isEmpty()
    • Checks if the list is empty or not
      • Returns true if the list is empty
      • Returns false if the list isn't empty
  • lastIndexOf(element)
    • Returns the last-placed of the mentioned element
      • Returns -1 if the element isn't prevalent in the list
  • remove(element)
    • Removes first-placed of the mentioned element in the list
  • remove(int index)
    • Removes an element from the specified index and shifts subsequent elements to the left along with decreasing indexes by 1
  • set(int index, element)
    • Replaces elements at a given index with the new element
    • Returns the newly replaced element as a new element
  • size()
    • Returns the list's size
  • sort(Comparator comp)
    • Sorts the elements in the list based on the mentioned comparator

ArrayList Example: Possible Data Structure for Students Signed Up for Website

import java.util.*;
public class JavaExample {
   public static void main(String args[]) {
      /* Creating ArrayList of type "String"*/
      ArrayList<String> obj = new ArrayList<String>();

      /*dds elements to the ArrayList*/
      obj.add("Rebecca");
      obj.add("Saathvika");
      obj.add("Sarayu");
      obj.add("Prisha");
      obj.add("William");
      obj.add("Vidhi");
      obj.add("Shreya");
      obj.add("Tianbin");

      // Displaying elements
      System.out.println("Starting Students Signed Up:");
      for(String str:obj)
         System.out.println(str); 
      
      System.out.println("the students are added to the arrayList");

      /* Adds element at the stated index in the list
       * obj.add(0, "Rohan") - Adding element "Rohan" to the first position in the list
       * obj.add(1, "Surya") - Adding element "Surya" to the second position in the list
       */

      obj.add(0, "Rohan");
      obj.add(1, "Surya");

      // Displaying elements of the new list
      System.out.println("Updated Students List (Additions):");
      for(String str:obj)
         System.out.println(str);

      //Remove elements from the ArrayList
      obj.remove("Sarayu"); //Removes "Sarayu" from the list
      obj.remove("Saathvika"); //Removes "Saathvika" from the list

      // This displays elements for the newer list
      System.out.println("Updated Students List (Removals):");
      for(String str:obj)
         System.out.println(str);

      //Removes the element from the index of the list
      obj.remove(1); //Removes the second element from the list

      // Displaying elements of the final most/most newly updated list
      System.out.println("Final Students List:");
      for(String str:obj)
         System.out.println(str); 
   }
   
} 
JavaExample.main(null);
Starting Students Signed Up:
Rebecca
Saathvika
Sarayu
Prisha
William
Vidhi
Shreya
Tianbin
the students are added to the arrayList
Updated Students List (Additions):
Rohan
Surya
Rebecca
Saathvika
Sarayu
Prisha
William
Vidhi
Shreya
Tianbin
Updated Students List (Removals):
Rohan
Surya
Rebecca
Prisha
William
Vidhi
Shreya
Tianbin
Final Students List:
Rohan
Rebecca
Prisha
William
Vidhi
Shreya
Tianbin