In this tutorial, we will explore the ArrayList
class in Java, which is a part of the List
interface. Unlike LinkedList
, ArrayList
provides better performance for random access operations compared to add and remove operations. This tutorial will demonstrate how to use ArrayList
with examples, covering all important operations and different ways for iteration using Java 8 features.
Table of Contents
- Introduction
- Prerequisites
- Step-by-Step Guide
- Creating an ArrayList
- Adding and Retrieving Elements
- Iterating Over the List
- Removing Elements
- ArrayList Methods
- Complete Code Example
- Conclusion
Introduction
ArrayList
is a part of Java's java.util
package and implements the List
interface. It provides a resizable array data structure. ArrayList
is useful in scenarios where frequent random access operations are required. It offers methods to manipulate elements dynamically.
Prerequisites
Before we start, ensure you have the following:
- Java Development Kit (JDK) installed (latest version preferred)
- An Integrated Development Environment (IDE) such as IntelliJ IDEA or Eclipse
Step-by-Step Guide
Step 1: Creating an ArrayList
First, let's create an ArrayList
and add some elements to it.
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Print the list
System.out.println("ArrayList: " + arrayList);
}
}
Output:
ArrayList: [Ravi, Sita, Arjun, Lakshmi]
Step 2: Adding and Retrieving Elements
Let's add some elements to the ArrayList
and retrieve elements using different methods.
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Retrieve and print elements
System.out.println("First element: " + arrayList.get(0));
System.out.println("Second element: " + arrayList.get(1));
}
}
Output:
First element: Ravi
Second element: Sita
Step 3: Iterating Over the List
We can iterate over the ArrayList
using a for-each loop, iterator, and Java 8 features like forEach and streams.
Using For-Each Loop
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Iterate over the list using for-each loop
System.out.println("Iterating over ArrayList using for-each loop:");
for (String element : arrayList) {
System.out.println(element);
}
}
}
Output:
Iterating over ArrayList using for-each loop:
Ravi
Sita
Arjun
Lakshmi
Using Iterator
import java.util.Iterator;
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Iterate over the list using iterator
System.out.println("Iterating over ArrayList using iterator:");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Output:
Iterating over ArrayList using iterator:
Ravi
Sita
Arjun
Lakshmi
Using forEach and Lambda Expression (Java 8)
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Iterate over the list using forEach and lambda
System.out.println("Iterating over ArrayList using forEach and lambda:");
arrayList.forEach(element -> System.out.println(element));
}
}
Output:
Iterating over ArrayList using forEach and lambda:
Ravi
Sita
Arjun
Lakshmi
Using Streams (Java 8)
import java.util.stream.Collectors;
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Iterate over the list using streams
System.out.println("Iterating over ArrayList using streams:");
arrayList.stream().forEach(System.out::println);
// Convert ArrayList to a List using streams
System.out.println("ArrayList to List:");
arrayList.stream().collect(Collectors.toList()).forEach(System.out::println);
}
}
Output:
Iterating over ArrayList using streams:
Ravi
Sita
Arjun
Lakshmi
ArrayList to List:
Ravi
Sita
Arjun
Lakshmi
Step 4: Removing Elements
Let's remove elements from the ArrayList
and demonstrate the use of remove
method.
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Remove an element by index
arrayList.remove(2);
// Remove an element by value
arrayList.remove("Lakshmi");
// Print the list after removal
System.out.println("ArrayList after removal: " + arrayList);
}
}
Output:
ArrayList after removal: [Ravi, Sita]
Step 5: ArrayList Methods
Here are some other important methods provided by the ArrayList
class:
add(int index, E element)
: Inserts the specified element at the specified position in this list.set(int index, E element)
: Replaces the element at the specified position in this list with the specified element.indexOf(Object o)
: Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.lastIndexOf(Object o)
: Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.clear()
: Removes all of the elements from this list.
import java.util.ArrayList;
import java.util.List;
public class ArrayListMethodsExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Add element at a specific position
arrayList.add(2, "Gopal");
// Replace element at a specific position
arrayList.set(1, "Ram");
// Get the index of an element
System.out.println("Index of Gopal: " + arrayList.indexOf("Gopal"));
// Get the last index of an element
arrayList.add("Ravi");
System.out.println("Last index of Ravi: " + arrayList.lastIndexOf("Ravi"));
// Clear the list
arrayList.clear();
System.out.println("ArrayList after clear: " + arrayList);
}
}
Output:
Index of Gopal: 2
Last index of Ravi: 4
ArrayList after clear: []
Complete Code Example
Here's the complete code example demonstrating various operations with ArrayList
:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
// Add elements to the list
arrayList.add("Ravi");
arrayList
.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Retrieve and print elements
System.out.println("First element: " + arrayList.get(0));
System.out.println("Second element: " + arrayList.get(1));
// Iterate over the list using for-each loop
System.out.println("Iterating over ArrayList using for-each loop:");
for (String element : arrayList) {
System.out.println(element);
}
// Iterate over the list using iterator
System.out.println("Iterating over ArrayList using iterator:");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// Iterate over the list using forEach and lambda
System.out.println("Iterating over ArrayList using forEach and lambda:");
arrayList.forEach(element -> System.out.println(element));
// Iterate over the list using streams
System.out.println("Iterating over ArrayList using streams:");
arrayList.stream().forEach(System.out::println);
// Convert ArrayList to a List using streams
System.out.println("ArrayList to List:");
arrayList.stream().collect(Collectors.toList()).forEach(System.out::println);
// Remove an element by index
arrayList.remove(2);
// Remove an element by value
arrayList.remove("Lakshmi");
// Print the list after removal
System.out.println("ArrayList after removal: " + arrayList);
// Add elements to the list
arrayList.add("Ravi");
arrayList.add("Sita");
arrayList.add("Arjun");
arrayList.add("Lakshmi");
// Add element at a specific position
arrayList.add(2, "Gopal");
// Replace element at a specific position
arrayList.set(1, "Ram");
// Get the index of an element
System.out.println("Index of Gopal: " + arrayList.indexOf("Gopal"));
// Get the last index of an element
arrayList.add("Ravi");
System.out.println("Last index of Ravi: " + arrayList.lastIndexOf("Ravi"));
// Clear the list
arrayList.clear();
System.out.println("ArrayList after clear: " + arrayList);
}
}
Output:
First element: Ravi
Second element: Sita
Iterating over ArrayList using for-each loop:
Ravi
Sita
Arjun
Lakshmi
Iterating over ArrayList using iterator:
Ravi
Sita
Arjun
Lakshmi
Iterating over ArrayList using forEach and lambda:
Ravi
Sita
Arjun
Lakshmi
Iterating over ArrayList using streams:
Ravi
Sita
Arjun
Lakshmi
ArrayList to List:
Ravi
Sita
Arjun
Lakshmi
ArrayList after removal: [Ravi, Sita]
Index of Gopal: 2
Last index of Ravi: 4
ArrayList after clear: []
Conclusion
In this tutorial, we demonstrated how to use the ArrayList
class in Java. We covered creating an ArrayList
, adding and retrieving elements, iterating over the list using various methods, removing elements, and using some important ArrayList
methods. By following this guide, developers can effectively use ArrayList
in scenarios where frequent random access operations are required.
Comments
Post a Comment
Leave Comment