//

ArrayList in java

ArrayList: It is a dynamic array which means we don’t need to give its size while creating it. Whenever we add or remove elements from an ArrayList its size dynamically increases or decreases.

ArrayList in Java

Initialization:

  1. ArrayList arr = new ArrayList();
  2. ArrayList arr = new ArrayList(Collection c);
  3. ArrayList arr = new ArrayList(n);
List<Type> list = new ArrayList<>();

or

ArrayList<Type> list = new ArrayList<>();

// Passing default size of arraylist
ArrayList<Type> list = new ArrayList<>(20); 

The default size of the array list: is 10
Load factor: 0.75 (when the list is filled to 75% it’s sized increases by 1.5 times)

ArrayList is implemented using Dynamic Array. Where when the load factor is encounted the size of the array is incresed x times.

Initialization

ArrayList<String> carsList = new ArrayList<>();
carsList.add("Porche 998 911");
carsList.add("Range Rover Evoque");
carsList.add("Range Rover Defender");
ArrayList<String> carsList = new ArrayList<String>() {
            {
                add("Porche 998 911");
                add("Range Rover Evoque");
                add("Range Rover Defender");
            }
        };
List<Integer> numbersList = Arrays.asList(1, 2, 3,4);

Methods

Methods:

  1. add(value) or add(index, value); :- O(n) in worst case scenario
  2. get(index) :- O(1) Constant ; returns element at given index
  3. remove(index) :- O(n); removes element at given index
  4. indexOf(Object o):- O(n); returns first index of passed object/element
  5. contains(Object o) : O(n); returns true is passed object/ element is present
  6. size(): returns the size of the arraylist

add() : adds and element to the list

addAll(): adds another list to the current list

remove(): removes an element from the list

contains(): checks whether arraylist contains the element

clear(): clears the list

indexOf()

lastIndexOf()

removeAll()

size()

toArray()

Ques: How ArrayList dynamically allocate memory?

Ans: ArrayList arr = new ArrayList(); Default constructor is used to create empty ArrayList of initial size 10.

#Load Factor of an ArrayList

Diff between Array and ArrayList

ArrayArrayList
Can’t update size once createdCan update size even after creation
Can hold primitive and objects bothCan only hold object uses Autoboxing to auto convert primitive to objects
Either can be 1D or 2DOnly 1D
staticdynamic
fasterslower

Questions:

  1. How to synchronize ArrayList in java?

Ans: By using Collection.synchronizedList() method

  1. Difference between length() of array and size() of ArrayList in Java?

Ans: length() : returns how many elements you can store in the array

size(): returns total number of elements stored in arraylist

  1. When to use Array and when to use Linked List
Java ArrayList Interview Questions and Answers
  1. Difference between ArrayList and Vector?

Ans: Both implement List interface but
ArrayList: Not synchronised, not thread safe and slow
Vector: Synchronised, thread safe and fast

Output Type Questions:

Question 1.

import java.util.ArrayList;

public class ArrayListTest {

public static void main(String[] args)

{

   ArrayList<String> list = new ArrayList<String>();

    list.add(null);

    list.add(0, “A”);

    list.add(3, “B”);

    list.add(1, “C”);

    System.out.println(list);

   }

}

Ans: It will compile but JVM will throw IndexOutOfBoundsException because we are adding “B” at location 3 which doesn’t exist.

Leave a Reply

Your email address will not be published. Required fields are marked *