ArrayList And LinkedList in Java

 ArrayList is a dynamic array used to store same or different type of data elements.

        - The elements in an array list can be accessed randomly using an index value
        - The insertion of data is non-synchronized
        - ArrayList implements List interface

Syntax:
        ArrayList<E> variable = new ArrayList<E>()  
        OR
        List<E> variable = new ArrayList<E>()

Example:
        List myList = new ArrayList();
        //add(element) is used to add an element in the list
        myList(1);
        myList("Hello");
        myList(true);

        for(int i=0; i<myList.size(); i++){
            //get(index) is used to access the list data at a specified index
            System.out.print(myList.get(i) + " ," );  //Output: 1, Hello, true
        }

        Observe the output: It is printing all the values of different data type

But it is not logical to store different elements of different data type in the same collection. That is why Generic is introduced. And using diamond(< >) operator, we can implement this feature to restrict used from inserting elements of different data type in the same collection.

        List<Integer> myList = new ArrayList<>(); 
        myList.add(1);
        myList.add(2);
        myList.add("hello") //In this line of code, compiler will throw an error as it is not an Integer value


Useful Methods of ArrayList
                ArrayList<Integer> arr = new ArrayList<>();
  • add(element): adds element in the list
               Example:
                            arr.add(12);
                            arr.add(2);
                            arr.add(5);
                            System.out.println(arr); //output: [12, 2, 5]
  • add(index, element): adds element at specified index
                            arr.add(1, 10); //will add element at index 1
                            System.out.println(arr); //output: [12, 10, 2, 5]
  • get(index): returns the element at the specified position in list if exists, otherwise throws IndexOutOfBoundsException at run time.
                            System.out.println(arr.get(3));  //output: 5
                            System.out.println(arr.get(2)); //Throws IndexOutOfBoundsException
  • isEmpty(): returns true if list is empty otherwise returns false
                             System.out.println(arr.isEmpty()); //false, because array cotains some elements
  • set(index, newElement): updates the element with the newElement at specified index
                           arr.set(1, 3);
                           System.out.println(arr); //output: [12, 3, 2, 5]
  • remove(index): removes element at specified index from the list if exists, otherwise throws an error
                            arr.remove(1);  //removes element at index 1 which is 3
                            System.out.println(arr); //[12, 2, 5]
  • size(): returns the number of elements present in the list
                            System.out.println(arr.size()); //returns 3, since array contains 3 elements 12, 2,5
  • clear(): removes all the elements from the list
                            arr.clear();
                            System.out.println(arr); //output: [ ]
   
       Note:   Converting List or ArrayList into an Java Array, we use toArray() methods

Linked List:
            LinkedList uses doubly linked list internally to store the elements. It maintains the insertion order.
       Syntax:
            LinkedList<E> variable = new LinkedList<E>();

       Note: Like ArrayList, insertion in LinkedList is also non-synchronized.
                - All the methods of ArrayList mentioned above will work similarly for LinkedList as well. But LinkedList includes some more useful methods:

       Let's understand some useful methods of LinkedList:
                LinkedList<String> names = new LinkedList<>();

  • addFirst(element): adds the element at the beginning of the list
  • addLast(element): add the element at the end of the list
  • getFirst(): returns the first element of the list if exists, otherwise throws   NoSuchElementException
  • getLast(): returns the last element of the list if exists, otherwise throws NoSuchElementException
Note: Similary we removeFirst() and removeLast() will remove element from beginning and end of the list respectively.

Continue Learning: PriorityQueue And ArrayDeques

Comments

Popular Posts