Java ArrayList

Java ArrayList:


Class Synatx:

java.lang.Object
     java.util.AbstractCollection
          java.util.AbstractList
               java.util.ArrayList
  • ArrayList is a part of collection framework.
  • The ArrayList class is a resizable array, which can be found in the java.util package.
  • Java ArrayList class uses a dynamic array for storing the elements. 
  • The difference between a built-in array and an ArrayList:
  • In, built-in array, the size of an array cannot be modified. 
  • While in ArrayList, elements can be added and removed whenever you want. 
  • Import Package: import java.util.ArrayList
Syntax: 
ArrayList = new ArrayList();

Example of string: 
ArrayList students = new ArrayList();

Example of Integer:
ArrayList myNumbers = new ArrayList();

Constructors in ArrayList:
  • ArrayList(): This constructor is used to build an empty array list
  • ArrayList(Collection c):  This constructor is used to build an array list initialized with the elements from collection c
  • ArrayList(int capacity):  This constructor is used to build an array list with initial capacity being specified
Advantages:
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list.
Methods:
  • How to add Items:
    • For example, to add elements to the ArrayList, use the add() method:
    • Syntax: Object-name.add(string);
    • Example: students.add("Sarthak");
    • Example: myNumbers .add(7);
  • How to find size of ArrayList
    • To find out how many elements an ArrayList have, use the size method:
    • Syntax: Object-name.size();
    • Example: students.size();
    • Example: myNumbers .size();
  • How to access an Item:
    • To access single items:
      • To access an element in the ArrayList, use the get() method and refer to the index number:
      • Syntax: Object-name.get(index);
      • Example: System.out.println(students.get(0));
      • Example: System.out.println(myNumbers.get(0));
    • To access multiple items using for loop:
      • Loop through the elements of an ArrayList with a for loop, and use the size() method to specify how many times the loop should run:
      • Example: 
                                     for (int i = 0; i < students.size(); i++) {
                                                 System.out.println(students.get(i));
                                         }
      • Example: 
                                     for (int i = 0; i < myNumbers.size(); i++) {
                                                 System.out.println(myNumbers.get(i));
                                         }
    • To access multiple items using for each loop:
      • Example:
                                         for (String i : students ) {
                                                      System.out.println(i);
                                              }
      • Example:
                                         for (Integer i : myNumbers) {
                                                      System.out.println(i);
                                              }
  • How to change an Item:
    • To modify an element, use the set() method and refer to the index number:
    • Syntax: Object-name.set(index,string);
    • Example: students.set(0, "Badal");
    • Example: myNumbers.set(0, 11);
  • How to remove an Item
    • Remove Single Item
      • To remove an element, use the remove() method and refer to the index number:
      • Syntax: Object-name.remove(index);
      • Example: students.remove(0);
      • Example: myNumbers.remove(0);
    • Remove all items
      • To remove all the elements in the ArrayList, use the clear() method:
      • Syntax: Object-name.clear();
      • Example: students.clear();
      • Example: myNumbers.clear();
  • Other Methods of Java ArrayList:
    • 1. boolean isEmpty() : It returns true if the list is empty, otherwise false.
    • 2. boolean contains(Object o) : It returns true if the list contains the specified element
    • 3. int indexOf(Object o) : It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form