0% found this document useful (0 votes)
2 views15 pages

ArrayList Methods

The document outlines various methods available in the ArrayList class in Java, including their descriptions, return types, and examples of usage. Key methods include add(), remove(), clear(), and sort(), which allow for manipulating the contents of an ArrayList. Each method is accompanied by a code snippet demonstrating its functionality.

Uploaded by

pankaj101babu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

ArrayList Methods

The document outlines various methods available in the ArrayList class in Java, including their descriptions, return types, and examples of usage. Key methods include add(), remove(), clear(), and sort(), which allow for manipulating the contents of an ArrayList. Each method is accompanied by a code snippet demonstrating its functionality.

Uploaded by

pankaj101babu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ArrayList Methods

1. add()
Description: Adds an item to the end of the list or at a specified index.

Return Type: boolean (when adding at a specific index) or void (when


appending to the end).

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple"); // Appends to the end
[Link](0, "Banana"); // Adds at index 0
[Link](fruits); // Output: [Banana, Apple]
}
}

2. addAll()
Description: Adds all elements from a specified collection to the end of the
list or at a specific index.

Return Type: boolean (returns true if the list was modified).

Example:

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
ArrayList<String> moreFruits = new ArrayList<>([Link]("Orang

ArrayList Methods 1
e", "Mango"));
[Link](moreFruits); // Adds all elements from moreFruits
[Link](fruits); // Output: [Apple, Orange, Mango]
}
}

3. clear()
Description: Removes all elements from the list, making it empty.

Return Type: void .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link](); // Removes all elements
[Link](fruits); // Output: []
}
}

4. clone()
Description: Creates a shallow copy of the ArrayList (copies the references
to the objects, not the objects themselves).

Return Type: Object (must be cast to ArrayList<T> ).

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();

ArrayList Methods 2
[Link]("Apple");
[Link]("Banana");
ArrayList<String> clonedList = (ArrayList<String>) [Link]();
[Link](clonedList); // Output: [Apple, Banana]
}
}

5. contains()
Description: Checks if the list contains a specific element (uses equals() for
comparison).

Return Type: boolean .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
boolean hasApple = [Link]("Apple");
[Link](hasApple); // Output: true
}
}

6. ensureCapacity()
Description: Increases the capacity of the ArrayList to ensure it can hold at
least the specified number of elements without resizing.

Return Type: void .

Example:

import [Link];

public class Main {

ArrayList Methods 3
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link](100); // Ensures capacity for 100 elements
[Link]("Apple");
[Link](fruits); // Output: [Apple]
}
}

7. forEach()
Description: Performs a specified action (via a Consumer ) on each element in
the list.

Return Type: void .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link](fruit -> [Link]([Link]()));
// Output:
// APPLE
// BANANA
}
}

8. get()
Description: Returns the element at the specified index.

Return Type: T (the type of the list's elements).

Example:

ArrayList Methods 4
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
String fruit = [Link](1);
[Link](fruit); // Output: Banana
}
}

9. indexOf()
Description: Returns the index of the first occurrence of a specified
element, or 1 if not found.

Return Type: int .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Apple");
int index = [Link]("Apple");
[Link](index); // Output: 0
}
}

10. isEmpty()
Description: Checks if the list contains no elements.

Return Type: boolean .

ArrayList Methods 5
Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]([Link]()); // Output: true
[Link]("Apple");
[Link]([Link]()); // Output: false
}
}

11. iterator()
Description: Returns an Iterator object to traverse the list's elements.

Return Type: Iterator<T> .

Example:

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
Iterator<String> iterator = [Link]();
while ([Link]()) {
[Link]([Link]());
}
// Output:
// Apple
// Banana
}
}

ArrayList Methods 6
12. lastIndexOf()
Description: Returns the index of the last occurrence of a specified
element, or 1 if not found.

Return Type: int .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Apple");
int lastIndex = [Link]("Apple");
[Link](lastIndex); // Output: 2
}
}

13. listIterator()
Description: Returns a ListIterator object, which allows bidirectional traversal
and modification of the list.

Return Type: ListIterator<T> .

Example:

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
ListIterator<String> listIterator = [Link]();
while ([Link]()) {

ArrayList Methods 7
[Link]([Link]());
}
// Output:
// Apple
// Banana
}
}

14. remove()
Description: Removes the element at a specified index or the first
occurrence of a specified element.

Return Type: T (when removing by index) or boolean (when removing by


object).

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link](0); // Removes element at index 0
[Link](fruits); // Output: [Banana]
[Link]("Banana"); // Removes first occurrence of "Banana"
[Link](fruits); // Output: []
}
}

15. removeAll()
Description: Removes all elements that are contained in a specified
collection.

Return Type: boolean (returns true if the list was modified).

Example:

ArrayList Methods 8
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
ArrayList<String> toRemove = new ArrayList<>([Link]("Apple",
"Orange"));
[Link](toRemove);
[Link](fruits); // Output: [Banana]
}
}

16. removeIf()
Description: Removes all elements that satisfy a given predicate
(condition).

Return Type: boolean (returns true if any elements were removed).

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Apricot");
[Link](fruit -> [Link]("A"));
[Link](fruits); // Output: [Banana]
}
}

ArrayList Methods 9
17. replaceAll()
Description: Replaces each element with the result of applying a specified
operation (via a UnaryOperator ).

Return Type: void .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link](String::toUpperCase);
[Link](fruits); // Output: [APPLE, BANANA]
}
}

18. retainAll()
Description: Removes all elements except those contained in a specified
collection.

Return Type: boolean (returns true if the list was modified).

Example:

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
ArrayList<String> toKeep = new ArrayList<>([Link]("Apple", "O
range"));

ArrayList Methods 10
[Link](toKeep);
[Link](fruits); // Output: [Apple, Orange]
}
}

19. set()
Description: Replaces the element at a specified index with a new element.

Return Type: T (returns the element previously at the specified index).

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
String oldFruit = [Link](1, "Orange");
[Link](fruits); // Output: [Apple, Orange]
[Link](oldFruit); // Output: Banana
}
}

20. size()
Description: Returns the number of elements in the list.

Return Type: int .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");

ArrayList Methods 11
[Link]("Apple");
[Link]([Link]()); // Output: 2
}
}

21. sort()
Description: Sorts the list according to a specified Comparator or in natural
order (if elements implement Comparable ).

Return Type: void .

Example:

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Banana");
[Link]("Apple");
[Link]("Orange");
[Link]([Link]());
[Link](fruits); // Output: [Apple, Banana, Orange]
}
}

22. spliterator()
Description: Returns a Spliterator object for traversing and partitioning the
list's elements, useful for parallel processing.

Return Type: Spliterator<T> .

Example:

import [Link];
import [Link];

ArrayList Methods 12
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
Spliterator<String> spliterator = [Link]();
[Link]([Link]::println);
// Output:
// Apple
// Banana
}
}

23. subList()
Description: Returns a view of the list between the specified fromIndex
(inclusive) and toIndex (exclusive). Changes to the sublist affect the original
list.

Return Type: List<T> .

Example:

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
List<String> fruits = [Link](1, 3);
[Link](subList); // Output: [Banana, Orange]
}
}

24. toArray()

ArrayList Methods 13
Description: Returns an array containing all elements of the list in order.

Return Type: Object[] or T[] (if a specific type is provided).

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
String[] fruitsArray = [Link](new String[0]);
[Link]([Link](fruitsArray)); // Output: [Apple, Ban
ana]
}
}

24. trimToSize()
Description: Trims the capacity of the ArrayList to match its current size,
removing excess capacity.

Return Type: void .

Example:

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>(100); // Initial capacity 100
[Link]("Apple");
[Link]("Banana");
[Link](); // Reduces capacity to match size (2)
[Link](fruits); // Output: [Apple, Banana]
}
}

ArrayList Methods 14
ArrayList Methods 15

You might also like