0% found this document useful (0 votes)
32 views

Java ArrayList

Java ArrayList allows for dynamic arrays where the size is not fixed. It inherits from the AbstractList class and implements the List interface. Key features include automatically increasing or decreasing size as elements are added or removed, allowing random access to elements via indexes, and not being synchronized. Common methods for ArrayLists include add() to insert elements, remove() to delete elements, get() to access elements, and iterators or for loops to traverse the list. Elements can also be changed using set() and lists can be sorted.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Java ArrayList

Java ArrayList allows for dynamic arrays where the size is not fixed. It inherits from the AbstractList class and implements the List interface. Key features include automatically increasing or decreasing size as elements are added or removed, allowing random access to elements via indexes, and not being synchronized. Common methods for ArrayLists include add() to insert elements, remove() to delete elements, get() to access elements, and iterators or for loops to traverse the list. Elements can also be changed using set() and lists can be sorted.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Java ArrayList

What is ArrayList in Java?


ArrayList is a Java class implemented using the List interface. Java ArrayList, as the name suggests, provides
the functionality of a dynamic array where the size is not fixed as an array. Also as a part of the Collection
framework, it has many features not available with arrays.
Example 1: The following implementation demonstrates how to create and
use an ArrayList with a mention of its size.
// Java program to demonstrate the working of ArrayList
import java.io.*; // Appending new elements at the end of the list
import java.util.*; for (int i = 1; i <= n; i++) {
arr1.add(i);
class ArrayListExample { arr2.add(i);
public static void main(String[] args) }// Printing the ArrayList
{ // Size of the ArrayList System.out.println("Array 1:
int n = 5; System.out.println("Array 2:
}
// Declaring the ArrayList with }
// initial size n
ArrayList<Integer> arr1 = new ArrayList<Integer>(n);
// Declaring the ArrayList
ArrayList<Integer> arr2 = new ArrayList<Integer>();
// Printing the ArrayList
System.out.println("Array 1:" + arr1);
System.out.println("Array 2:" + arr2);
Important Features of ArrayList in Java
• ArrayList inherits AbstractList class and implements the List interface.
• ArrayList is initialized by size. However, the size is increased automatically if the
collection grows or shrinks if the objects are removed from the collection.
• Java ArrayList allows us to randomly access the list.
• ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class
for such cases.
• ArrayList in Java can be seen as a vector in C++.
• ArrayList is not Synchronized. Its equivalent synchronized class in Java is Vector.
Constructors in ArrayList

 In order to create an ArrayList, we need to create an object of the ArrayList class. The
ArrayList class consists of various constructors which allow the possible creation of the
array list. The following are the constructors available in this class:
 1. ArrayList()
 This constructor is used to build an empty array list. If we wish to create an empty
ArrayList with the name arr, then, it can be created as:

 2. ArrayList(int capacity)
 This constructor is used to build an array list with the initial capacity being specified.
Suppose we wish to create an ArrayList with the initial size being N, then, it can be created
as:
ArrayList in Java methods
1. Adding Elements

 In order to add an element to an ArrayList, we can use the add() method. This method is
overloaded to perform multiple operations based on different parameters. They are as
follows:
• add(Object): This method is used to add an element at the end of the ArrayList.
• add(int index, Object): This method is used to add an element at a specific index in the
ArrayList.
 Below is the implementation of the above approach:
 // Java Program to Add elements to An ArrayList
 // Importing all utility classes
 import java.util.*;
 class GFG {
 public static void main(String args[])
 {
 // Creating an Array of string type
 ArrayList<String> al = new ArrayList<>();
 // Adding elements to ArrayList Custom inputs
 al.add("Geeks");
 al.add("Geeks");
 // Here we are mentioning the index at which it is to be added
 al.add(1, "For");
 // Printing all the elements in an ArrayList
 System.out.println(al);
 }
2. Changing Elements

 After adding the elements, if we wish to change the element, it can be done using the set()
method. Since an ArrayList is indexed, the element which we wish to change is referenced
by the index of the element. Therefore, this method takes an index and the updated element
which needs to be inserted at that index.
 Below is the implementation of the above approach:
 // Java Program to Change elements in ArrayList Importing all utility classes
 import java.util.*;
 class GFG {
 // Main driver method
 public static void main(String args[])
 {
 // Creating an Arraylist object of string type
 ArrayList<String> al = new ArrayList<>();
 // Adding elements to Arraylist Custom input elements
 al.add("Geeks");
 al.add("Geeks");
 // Adding specifying the index to be added
 al.add(1, "Geeks");
 // Printing the Arraylist elements
 System.out.println("Initial ArrayList " + al);
 // Setting element at 1st index
 al.set(1, "For");
 // Printing the updated Arraylist
 System.out.println("Updated ArrayList " + al);
 }
3. Removing Elements

 In order to remove an element from an ArrayList, we can use the remove() method. This
method is overloaded to perform multiple operations based on different parameters. They
are as follows:
• remove(Object): This method is used to simply remove an object from the ArrayList. If
there are multiple such objects, then the first occurrence of the object is removed.
• remove(int index): Since an ArrayList is indexed, this method takes an integer value
which simply removes the element present at that specific index in the ArrayList. After
removing the element, all the elements are moved to the left to fill the space and the
indices of the objects are updated.
 // Java program to Remove Elements in ArrayList Importing all utility classes
 import java.util.*;
 class GFG {
 // Main driver method
 public static void main(String args[])
 {
 // Creating an object of arraylist class
 ArrayList<String> al = new ArrayList<>();
 // Adding elements to ArrayList Custom addition
 al.add("Geeks");
 al.add("Geeks");
 // Adding element at specific index
 al.add(1, "For");
 // Printing all elements of ArrayList
 System.out.println("Initial ArrayList " + al);
 // Removing element from above ArrayList
 al.remove(1);
 // Printing the updated Arraylist elements
 System.out.println("After the Index Removal " + al);
 // Removing this word element in ArrayList
 al.remove("Geeks");
 // Now printing updated ArrayList
 System.out.println("After the Object Removal "+ al);
 }
4. Iterating the ArrayList

 There are multiple ways to iterate through the ArrayList. The most famous ways are by
using the basic for loop in combination with a get() method to get the element at a specific
index and the advanced for a loop.
 // Java program to Iterate the elements in an ArrayList
 // Importing all utility classes
System.out.println();
 import java.util.*; // Using the for each loop
 class GFG { for (String str : al)
System.out.print(
 // Main driver method }
 public static void main(String args[]) }
 {
 // Creating an Arraylist of string type
 ArrayList<String> al = new ArrayList<>();
 // Adding elements to ArrayList using standard add() method
 al.add("Geeks");
 al.add("Geeks");
 al.add(1, "For");
 // Using the Get method and the for loop
 for (int i = 0; i < al.size(); i++) {
 System.out.print(al.get(i) + " ");
 }
5. Get Elements
 // Java program to get the elemens in ArrayList
 import java.io.*;
 import java.util.*;

 class GFG {
 public static void main (String[] args) {
 ArrayList<Integer> list = new ArrayList();
 // add the number
 list.add(9);
 list.add(5);
 list.add(6);
 System.out.println(list);
 // get method
 Integer n= list.get(1);
 System.out.println("at indext 1 number is:"+n);
 }
6.//Add

Elements Between Two Numbers
Java program to add the elements between two numbers in ArrayList
 import java.io.*;
 import java.util.*;
 class GFG {
 public static void main(String[] args)
 {
 ArrayList<Integer> list = new ArrayList();
 list.add(1);
 list.add(2);
 list.add(4);
 System.out.println(list);
 // insert missing element 3
 list.add(2, 3);
 System.out.println(list);
 }
7. ArrayList Sort
 // Java Program for ArrayList Sorting
 import java.io.*;
 import java.util.*;
 class GFG {
 public static void main(String[] args)
 {
 ArrayList<Integer> list = new ArrayList();
 list.add(2);
 list.add(4);
 list.add(3);
 list.add(1);
 System.out.println("Before sorting list:");
 System.out.println(list);
 Collections.sort(list);
 System.out.println("after sorting list:");
 System.out.println(list);
 }
8. Size of Elements
 // Java program to find the size
 // of elements of an ArrayList
 import java.io.*;
 import java.util.*;
 class GFG {
 public static void main(String[] args)
 {
 ArrayList<Integer> list = new ArrayList();

 list.add(1);
 list.add(2);
 list.add(3);
 list.add(4);
 int b = list.size();
 System.out.println("The size is :" + b);
 }
Advantages of ArrayList

1. Dynamic size: ArrayList can dynamically grow and shrink in size, making it easy to add or
remove elements as needed.
2. Easy to use: ArrayList is simple to use, making it a popular choice for many Java
developers.
3. Fast access: ArrayList provides fast access to elements, as it is implemented as an array
under the hood.
4. Ordered collection: ArrayList preserves the order of elements, allowing you to access
elements in the order they were added.
5. Supports null values: ArrayList can store null values, making it useful in cases where the
absence of a value needs to be represented.
Disadvantages of ArrayList

1. Slower than arrays: ArrayList is slower than arrays for certain operations, such as inserting
elements in the middle of the list.
2. Increased memory usage: ArrayList requires more memory than arrays, as it needs to
maintain its dynamic size and handle resizing.
3. Not thread-safe: ArrayList is not thread-safe, meaning that multiple threads may access and
modify the list concurrently, leading to potential race conditions and data corruption.
4. Performance degradation: ArrayList’s performance may degrade as the number of elements
in the list increases, especially for operations such as searching for elements or inserting
elements in the middle of the list.

You might also like