Array List

ArrayList in Java

What Is It?
An ArrayList is a type of list that allows you to store elements in a resizable array format in Java. Unlike standard arrays, ArrayLists can grow and shrink in size dynamically.

How It Works
ArrayList is part of Java’s java.util package, which means you need to import it before using it. It can store any type of objects, and the size adjusts as you add or remove elements.

Syntax for Creating an ArrayList
Here’s how to create an ArrayList:

ArrayList<Type> arrayList= new ArrayList<>()

//Here, Type indicates the type of an arraylist.

//For example

// create Integer type arraylist

ArrayList<Integer> arrayList = new ArrayList<>();

// create String type arraylist

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

In the provided code snippets, we used Integer instead of int when declaring the ArrayList. This is because ArrayLists in Java cannot directly store primitive data types; they can only hold objects. To work around this limitation, we use the corresponding wrapper classes, which are objects that encapsulate primitive types.

Important characteristics of the ArrayList:

  1. Resizable Array: ArrayList is based on a resizable array or growable array data structure.
  2. Duplicate Elements: ArrayList allows duplicate elements, meaning the same value can appear multiple times in the list.
  3. Preserved Insertion Order: The order in which elements are inserted into the ArrayList is maintained, and elements can be accessed based on their index.
  4. Heterogeneous Objects: ArrayList can hold elements of different data types, allowing for a mix of various objects in the same list.
  5. Null Insertion: It is possible to insert “null” values into an ArrayList.

Useful methods in ArrayList

ArrayList provides a variety of useful methods to manage elements within the list. Some of the most commonly used methods include:

  1. Adding Elements:
    • add(E element): Appends the specified element to the end of the ArrayList.
    • add(int index, E element): Inserts the element at the specified index, shifting existing elements to the right.
  2. Accessing Elements:
    • get(int index): Retrieves the element at the specified index.
    • indexOf(Object o): Returns the index of the first occurrence of the specified element, or -1 if not found.
    • lastIndexOf(Object o): Returns the index of the last occurrence of the specified element, or -1 if not found.
  3. Removing Elements:
    • remove(int index): Removes the element at the specified index.
    • remove(Object o): Removes the first occurrence of the specified element, if present.
    • clear(): Removes all elements from the ArrayList, making it empty.
  4. Size and Capacity:
    • size(): Returns the number of elements in the ArrayList.
    • isEmpty(): Checks if the ArrayList is empty.
    • ensureCapacity(int minCapacity): Increases the capacity of the ArrayList to at least the given minimum capacity.
    • trimToSize(): Reduces the internal array size to the actual number of elements in the list.
  5. Iterating through ArrayList:
    • Using a for loop, for-each loop, or an iterator to traverse the elements in the list.
  6. List Iteration and Modification:
    • listIterator(): Returns a list iterator over the elements in the list.
    • listIterator(int index): Returns a list iterator over the elements, starting at the specified index.
  7. Sublist Operations:
    • subList(int fromIndex, int toIndex): Returns a view of the portion of the ArrayList between the specified fromIndex, inclusive, and toIndex, exclusive.

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close