Java Intermediate

0% completed

Previous
Next
ArrayList Operations

ArrayList is a versatile and commonly used implementation of the List interface in Java. In this lesson, we will explore several basic operations that can be performed on an ArrayList. These operations include updating elements, inserting elements at a specific position, removing elements, and sorting the list. Each section below provides the syntax for the operation along with an example and a brief explanation.

1. Updating an Element

Updating an element in an ArrayList means replacing the element at a specified index with a new value. The set() method is used for this purpose.

Syntax

list.set(int index, E element);
  • Parameters:
    • index: The position at which the element should be replaced.
    • element: The new element that will replace the existing element.

Example

Java
Java

. . . .

In the line:

List<String> fruits = new ArrayList<String>();

we are declaring fruits as a List<String> but initializing it as an ArrayList<String>. The reason for using List instead of ArrayList on the left side is to follow the principle of programming to an interface rather than a concrete implementation.

Why use List<String>?

  1. Flexibility – If in the future, we decide to change ArrayList to another implementation of List (such as LinkedList), we only need to change the right-hand side:

    List<String> fruits = new LinkedList<String>();

    If we had used ArrayList<String> on the left, changing the type would require modifying more parts of the code.

  2. Code Maintainability – Using List makes it easier to switch implementations without affecting the code that interacts with fruits.

  3. PolymorphismList is an interface, and ArrayList is one of its implementations. By referencing the object with List, we ensure that our code is not tightly coupled to a specific implementation.

When should we use ArrayList<String> explicitly?

If we need to use methods that are specific to ArrayList (and not part of List), then we might declare it explicitly:

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

However, in most cases, it's best to use List<String> for flexibility.

2. Inserting an Element at a Specific Position

To insert an element at a specific position without replacing the existing element, use the add(int index, E element) method. This shifts subsequent elements to the right.

Syntax

list.add(int index, E element);
  • Parameters:
    • index: The position at which to insert the new element.
    • element: The element to be inserted.

Example

Java
Java

. . . .

Example Explanation:

  • The ArrayList fruits is created with "Apple" and "Cherry".
  • The add(1, "Banana") call inserts "Banana" at index 1, shifting "Cherry" to index 2.
  • The updated list is printed, showing the insertion order.

3. Removing an Element

Removing an element from an ArrayList can be done by specifying either the index or the element itself using the remove() method. Removing by index shifts subsequent elements to the left.

Syntax

  • By Index:

    list.remove(int index);
  • By Element:

    list.remove(Object o);

Example

Java
Java

. . . .

Example Explanation:

  • Initially, the list contains "Apple", "Banana", and "Cherry".
  • The call remove(1) deletes the element at index 1 ("Banana"), and the list becomes ["Apple", "Cherry"].
  • Next, remove("Cherry") removes the element "Cherry" by value, leaving ["Apple"].
  • The list is printed after each removal to show the changes.

4. Sorting the ArrayList

Sorting an ArrayList is a common operation to arrange its elements in natural order or according to a custom order. The Collections.sort() method is typically used for sorting. This method modifies the list in place.

Syntax

Collections.sort(list);
  • Explanation:
    The Collections.sort() method sorts the list in its natural order (for elements that implement the Comparable interface). You can also pass a custom Comparator if needed.

Example

Java
Java

. . . .

Example Explanation:

  • The ArrayList fruits is created with elements out of order.
  • The Collections.sort(fruits) call sorts the list alphabetically.
  • The sorted list is printed, showing the elements in natural order.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next