ArrayList in java

In this article, we will learn about ArrayList in java. ArrayList is part of the collection framework and is available in the java.util.package. It provides dynamic arrays in Java. However, it can be slower than standard arrays but can be useful in programs that require a lot of manipulation of the array. This class is in the java.util package.

Example:  how to create and use an ArrayList.

import java.io.*;
import java.util.*;

class ArrayList {
	public static void main(String[] args)
	{
		// Size of the ArrayList
		int n = 5;

		// Declaring the ArrayList with initial size n
		ArrayList<Integer> arrli
			= new ArrayList<Integer>(n);

		// Appending new elements at the end of the list
		for (int i = 1; i <= n; i++)
			arrli.add(i);
                System.out.println(arrli);

		// Remove element at index 3
		arrli.remove(3);

		// Displaying the ArrayList after deletion
		System.out.println(arrli);

		// Printing one by one
		for (int i = 0; i < arrli.size(); i++)
			System.out.print(arrli.get(i) + " ");
	}
}

Output:

[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5

Other operations on ArrayList like Adding Elements, Changing Elements, Removing Elements, and Iterating the ArrayList. Some examples are

Adding Elements

To add an element to an ArrayList, we can use the add() method. Base on different parameters, this method is overload to perform multiple operations. They are:

add (Object): At the end of the ArrayList this method is use to add an element
add (int index, Object): this method is used to add an element to a specific index in the ArrayList.

Example

import java.util.*;
public class addElement{

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

		al.add("Handy");
		al.add("Add Element Article");
		al.add(1, "Opinion");

		System.out.println(al);
	}
}

Output:

[Handy, Opinion, Add Element Article]

Changing Elements

If after adding the elements we want to change the element, we can do it with the set () method. Since an ArrayList is indexed, the element we want to change is referenced by the index of the element. Therefore, this method uses an index and the updated item to insert at that index.

Example

import java.util.*;

public class changeElement{

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

		al.add("Handy");
		al.add("Handy");
		al.add(1, "Handy");

		System.out.println("Initial ArrayList " + al);

		al.set(1, "Opinion");

		System.out.println("Updated ArrayList " + al);
	}
}

Output:

Initial ArrayList [Handy, Handy, Handy]
Updated ArrayList [Handy, Opinion, Handy]

That’s all we will learn about ArrayList in java.

Next Article

1. PriorityQueue in Java
2. Stack Class in Java
3. Vector Class in Java
4. Queue Interface In Java
5. List Interface in Java with Examples

Please share this post:
Posts created 72

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: