ArrayList in Java
๐งญ ArrayList in Java
In Java, an ArrayList is a dynamic array that can grow and shrink in size during program execution.
ArrayList is one of the most widely used classes in the Java Collections Framework.
๐น What is an ArrayList?
An ArrayList in Java is a resizable array that can hold objects. It automatically increases its capacity when elements are added beyond its current size.
It belongs to the package:
ArrayList can store duplicate elements, maintains insertion order, and allows random access using indexes.
๐น Syntax to Create an ArrayList
Example:
๐น Key Features of ArrayList
✅ Dynamic size – Grows or shrinks automatically
✅ Maintains insertion order
✅ Allows duplicate elements
✅ Provides random access using indexes
✅ Not synchronized – faster but not thread-safe
✅ Part of Java Collections Framework
๐น Adding Elements to an ArrayList
You can add elements using the add()
method.
Output:
๐น Accessing Elements in an ArrayList
You can use the get(index)
method to access elements.
Or loop through all elements:
๐น Updating and Removing Elements
Update:
Output:
Remove:
Output:
๐น Example 1: Basic ArrayList Operations
Output:
๐น Iterating Through an ArrayList
You can traverse an ArrayList using:
1️⃣ For Loop
2️⃣ Enhanced For Loop
3️⃣ Iterator
๐น Common ArrayList Methods
Method | Description | Example |
---|---|---|
add(element) | Adds an element | list.add(10) |
add(index, element) | Adds at a specific position | list.add(1, 25) |
get(index) | Returns element | list.get(2) |
set(index, element) | Replaces element | list.set(0, 99) |
remove(index) | Removes element | list.remove(1) |
clear() | Removes all elements | list.clear() |
size() | Returns number of elements | list.size() |
contains(value) | Checks if value exists | list.contains(30) |
isEmpty() | Checks if list is empty | list.isEmpty() |
๐น Example 2: Using ArrayList Methods
Output:
๐น ArrayList vs Vector
Feature | ArrayList | Vector |
---|---|---|
Synchronization | ❌ Not synchronized | ✅ Synchronized |
Performance | ⚡ Faster | ⚙️ Slower |
Growth rate | Increases by 50% | Doubles in size |
Introduced in | JDK 1.2 | JDK 1.0 |
Use case | Single-threaded programs | Multi-threaded programs |
๐น Example 3: Copy All Elements from One ArrayList to Another
Output:
๐น Example 4: Sorting Elements in an ArrayList
Output:
๐น Example 5: Searching for an Element in an ArrayList
Output:
๐ก Summary
Concept | Description |
---|---|
ArrayList | Dynamic array in Java |
Resizable | Grows automatically as elements are added |
Not synchronized | Faster than Vector |
Common methods | add() , get() , set() , remove() , size() |
Belongs to | java.util package |
๐งพ Key Takeaway
The ArrayList is one of the most important and flexible data structures in Java.
It provides the efficiency of arrays combined with the dynamic behavior of linked structures — making it ideal for most modern Java applications.
Example Programs
1️⃣ Create an ArrayList of 5 Numbers and Find Their Average
Output:
2️⃣ Write a Program to Remove Duplicate Elements from an ArrayList
Output:
3️⃣ Write a Program to Reverse an ArrayList
Output:
4️⃣ Create an ArrayList of Strings and Find the Longest String
Output:
5️⃣ Write a Program to Merge Two ArrayLists and Display the Result
Output:
Comments
Post a Comment