0% completed
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.
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.
list.set(int index, E element);
index
: The position at which the element should be replaced.element
: The new element that will replace the existing element.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.
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.
Code Maintainability – Using List
makes it easier to switch implementations without affecting the code that interacts with fruits
.
Polymorphism – List
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.
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.
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.
list.add(int index, E element);
index
: The position at which to insert the new element.element
: The element to be inserted.Example Explanation:
fruits
is created with "Apple" and "Cherry".add(1, "Banana")
call inserts "Banana" at index 1, shifting "Cherry" to index 2.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.
By Index:
list.remove(int index);
By Element:
list.remove(Object o);
Example Explanation:
remove(1)
deletes the element at index 1 ("Banana"), and the list becomes ["Apple", "Cherry"].remove("Cherry")
removes the element "Cherry" by value, leaving ["Apple"].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.
Collections.sort(list);
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 Explanation:
fruits
is created with elements out of order.Collections.sort(fruits)
call sorts the list alphabetically......
.....
.....