[go: up one dir, main page]

0% found this document useful (0 votes)
20 views6 pages

ArrayList ND some methods

Uploaded by

jeeteshrapeti
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)
20 views6 pages

ArrayList ND some methods

Uploaded by

jeeteshrapeti
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
You are on page 1/ 6

ArrayList ND some methods

What is an ArrayList in Java?


ArrayList is a resizable array implementation in Java's java.util package. Unlike regular arrays, its size
can grow or shrink dynamically, making it more flexible when the exact size of the data is not known
in advance.
Syntax to Declare and Initialize an ArrayList
import java.util.ArrayList;

ArrayList<Type> listName = new ArrayList<Type>();

Key Points
Dynamic Resizing: Automatically grows as elements are added.
Generic: Allows specifying the type of elements ( <Type> ) it stores.
Random Access: Elements can be accessed directly by their index.
Performance: Fast for random access but slower for operations like insertion/removal in the
middle.
When to Use?
Use ArrayList when the size of the array can vary during runtime.
Prefer ArrayList over arrays if frequent insertion, deletion, or retrieval operations are required.
Why Use?
Flexible size handling.
Part of the Collections Framework, providing additional utility methods.

How to Use?
1. Import java.util.ArrayList .
2. Create an instance of ArrayList specifying the data type.
3. Use its methods to manipulate the collection.

Important Methods of ArrayList


Method Explanation
add(E e) Adds an element to the end of the list.
Method Explanation
add(int index, E element) Inserts an element at a specified index.
get(int index) Returns the element at the specified position.
set(int index, E element) Replaces the element at the specified position.
remove(int index) Removes the element at the specified index.
remove(Object o) Removes the first occurrence of the specified element.
size() Returns the number of elements in the list.
isEmpty() Checks if the list is empty.
clear() Removes all elements from the list.
contains(Object o) Checks if the list contains the specified element.
indexOf(Object o) Returns the index of the first occurrence of the specified element.
lastIndexOf(Object o) Returns the index of the last occurrence of the specified element.
toArray() Converts the list to an array.
subList(int fromIndex, int toIndex) Returns a view of a portion of the list.
iterator() Returns an iterator for the elements of the list.

Code Example: Working with ArrayList


import java.util.ArrayList;

public class ArrayListExample {


public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList<String> fruits = new ArrayList<>();

// Add elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");

// Insert an element at index 1


fruits.add(1, "Orange");

// Access elements
System.out.println("Element at index 2: " + fruits.get(2)); // Cherry

// Replace an element
fruits.set(2, "Grapes");

// Remove an element by index


fruits.remove(1);

// Check if a list contains an element


System.out.println("Contains 'Banana': " + fruits.contains("Banana")); // true

// Iterate over the list


System.out.println("Fruits list:");
for (String fruit : fruits) {
System.out.println(fruit);
}

// Clear the list


fruits.clear();
System.out.println("Is the list empty? " + fruits.isEmpty()); // true
}
}

1. add(E e)
Adds an element to the end of the list.

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


list.add("Apple");
System.out.println(list); // Output: [Apple]

2. add(int index, E element)


Inserts an element at a specified index.

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


list.add("Apple");
list.add(0, "Orange");
System.out.println(list); // Output: [Orange, Apple]

3. get(int index)
Returns the element at the specified index.

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


list.add("Apple");
System.out.println(list.get(0)); // Output: Apple

4. set(int index, E element)


Replaces the element at the specified index.

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


list.add("Apple");
list.set(0, "Banana");
System.out.println(list); // Output: [Banana]

5. remove(int index)
Removes the element at the specified index.

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


list.add("Apple");
list.remove(0);
System.out.println(list); // Output: []

6. remove(Object o)
Removes the first occurrence of the specified element.

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


list.add("Apple");
list.add("Banana");
list.remove("Apple");
System.out.println(list); // Output: [Banana]

7. size()
Returns the number of elements in the list.

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


list.add("Apple");
System.out.println(list.size()); // Output: 1

8. isEmpty()
Checks if the list is empty.
ArrayList<String> list = new ArrayList<>();
System.out.println(list.isEmpty()); // Output: true

9. clear()
Removes all elements from the list.

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


list.add("Apple");
list.clear();
System.out.println(list); // Output: []

10. contains(Object o)
Checks if the list contains the specified element.

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


list.add("Apple");
System.out.println(list.contains("Apple")); // Output: true

11. indexOf(Object o)
Returns the index of the first occurrence of the specified element.

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


list.add("Apple");
list.add("Banana");
System.out.println(list.indexOf("Banana")); // Output: 1

12. lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element.

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


list.add("Apple");
list.add("Banana");
list.add("Apple");
System.out.println(list.lastIndexOf("Apple")); // Output: 2
13. toArray()
Converts the list to an array.

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


list.add("Apple");
list.add("Banana");
Object[] array = list.toArray();
System.out.println(Arrays.toString(array)); // Output: [Apple, Banana]

14. subList(int fromIndex, int toIndex)


Returns a view of a portion of the list.

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


list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list.subList(0, 2)); // Output: [Apple, Banana]

15. iterator()
Returns an iterator for the list.

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


list.add("Apple");
list.add("Banana");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// Output:
// Apple
// Banana

You might also like