Java ArrayList
ArrayList in Java is a part of the [Link] package. It is a resizable array, which means elements
can be added or removed dynamically.
1. Creating an ArrayList
To create an ArrayList, you need to import [Link] and then instantiate it.
Example:
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
// Creating an ArrayList of integers
ArrayList<Integer> intList = new ArrayList<>();
// Creating an ArrayList of strings
ArrayList<String> stringList = new ArrayList<>();
}
}
2. Inserting Values
You can add values using the add method.
Example:
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<>();
[Link](10); // Adding an integer
[Link](20);
[Link](30);
ArrayList<String> stringList = new ArrayList<>();
[Link]("Hello"); // Adding a string
[Link]("World");
// Printing the ArrayLists
[Link]("Integer List: " + intList);
[Link]("String List: " + stringList);
}
}
3. Deleting Values
You can remove values using the remove method by index or by value.
Example:
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
// Removing value by index
[Link](1); // Removes the element at index 1 (20)
// Removing value by value (first occurrence)
[Link]([Link](30));
// Printing the updated list
[Link]("Updated Integer List: " + intList);
}
}
4. Reversing an ArrayList
You can reverse an ArrayList using [Link] method.
Example:
import [Link];
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
// Reversing the list
[Link](intList);
// Printing the reversed list
[Link]("Reversed Integer List: " + intList);
}
}
5. Sorting an ArrayList
You can sort an ArrayList in ascending order using [Link] method.
Example:
import [Link];
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<>();
[Link](30);
[Link](10);
[Link](20);
// Sorting the list in ascending order
[Link](intList);
// Printing the sorted list
[Link]("Sorted Integer List: " + intList);
}
}
9. The ArrayList class does not have a built-in method named merge. However, you can merge
two ArrayLists manually by using the addAll() method or iterating over one list and adding its
elements to another.
Here’s how you can merge two ArrayLists in Java:
Using addAll()
The addAll() method adds all the elements from one list to another.
Example:
import [Link];
public class MergeExample {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
[Link](1);
[Link](2);
[Link](3);
ArrayList<Integer> list2 = new ArrayList<>();
[Link](4);
[Link](5);
[Link](6);
// Merging list2 into list1
[Link](list2);
// Printing the merged list
[Link]("Merged List: " + list1);
ADDITIONAL
ArrayList Methods
1. Adding Elements
● add(E e): Adds an element at the end.
● add(int index, E e): Inserts an element at the specified index.
2. Accessing Elements
● get(int index): Retrieves the element at the specified index.
ArrayList<Integer> list = new ArrayList<>();
[Link](10);
int element = [Link](0); // element = 10
3. Updating Elements
● set(int index, E element): Replaces the element at the specified index with the
specified element.
[Link](0, 20); // Replaces the value at index 0 with 20
4. Removing Elements
● remove(int index): Removes the element at the specified index.
● remove(Object o): Removes the first occurrence of the specified element.
● clear(): Removes all elements from the list
[Link](); // Empties the list
5. Searching Elements
● contains(Object o): Checks if the list contains the specified element.
boolean exists = [Link](10); // true if 10 exists
indexOf(Object o): Returns the index of the first occurrence of the element, or -1 if not
found.
lastIndexOf(Object o): Returns the index of the last occurrence of the element.
6. Size and Capacity
● size(): Returns the number of elements in the list.
int size = [Link](); // Gets the number of elements
ensureCapacity(int minCapacity): Ensures the capacity is at least the specified size.
trimToSize(): Reduces the storage capacity of the ArrayList to its current size.
7. Iterating Through the List
● forEach(Consumer<? super E> action): Performs the specified action for each
element.
[Link]([Link]::println); // Prints each element
iterator(): Returns an iterator for the list.
listIterator(): Returns a list iterator to traverse the list in both directions.
8. Sublist and Views
● subList(int fromIndex, int toIndex): Returns a view of the portion of the list between
the specified indices.
List<Integer> sublist = [Link](0, 2); // Sublist of first two elements
9. Comparing Lists
● equals(Object o): Compares the list with another object for equality.
10. Other Utility Methods
● isEmpty(): Checks if the list is empty
boolean empty = [Link](); // true if the list is empty
Object[] array = [Link]();
toArray(T[] a): Converts the list into the specified type of array.