methods specific to list interface
The `List` interface in Java extends the `Collection` interface and adds several methods that are specific to dealing with ordered collections. These methods allow manipulation of elements based on their index positions within the list. Here are the key methods specific to the `List` interface:
Table of Contents
1. Adding and Removing Elements at Specific Positions:
- `void add(int index, E element)`: Inserts the specified element at the specified position in the list, shifting the subsequent elements to the right.
- `E remove(int index)`: Removes the element at the specified position in the list, shifting any subsequent elements to the left.
- `boolean addAll(int index, Collection<? extends E> c)`: Inserts all of the elements in the specified collection into the list at the specified position, preserving the order of elements.
2. Accessing Elements by Index:
- `E get(int index)`: Returns the element at the specified position in the list.
- `E set(int index, E element)`: Replaces the element at the specified position in the list with the specified element.
- `int indexOf(Object o)`: Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.
- `int lastIndexOf(Object o)`: Returns the index of the last occurrence of the specified element in the list, or -1 if the list does not contain the element.
3. Sublist Operations:
- `List<E> subList(int fromIndex, int toIndex)`: Returns a view of the portion of this list between the specified `fromIndex`, inclusive, and `toIndex`, exclusive.
4. List Iteration:
- `ListIterator<E> listIterator()`: Returns a list iterator over the elements in the list, starting at the beginning of the list.
- `ListIterator<E> listIterator(int index)`: Returns a list iterator over the elements in the list, starting at the specified position in the list.
Example Usage of List Interface Methods
```java
import java.util.List;
import java.util.ArrayList;
public class ListMethodsExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
// Adding elements at specific positions
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add(1, "Orange"); // Inserts "Orange" at index 1
// Removing an element at a specific position
String removedElement = list.remove(2); // Removes "Banana" at index 2
// Getting an element by index
String elementAtIndex = list.get(0); // Gets the element at index 0
// Replacing an element at a specific position
String replacedElement = list.set(1, "Grapes"); // Replaces element at index 1 with "Grapes"
// Finding the index of an element
int index = list.indexOf("Cherry"); // Gets the index of "Cherry"
// Sublist operations
List<String> subList = list.subList(1, 3); // Gets a sublist from index 1 to 2
// List iteration
System.out.println("List elements:");
for (String element : list) {
System.out.println(element);
}
// List iteration with ListIterator
System.out.println("List elements using ListIterator:");
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
```
These methods make the `List` interface powerful for manipulating ordered collections, allowing developers to perform various operations such as adding, removing, accessing, and iterating over elements with ease.