Introduction to ArrayList in JAVA
ArrayList is used to create dynamic arrays in Java programming. In the implementation of ArrayList class, this class extends the AbstractList and implement and interface named list interface. This type of arrays is like linked list, it grows and shrinks as the data increases and decreases in the array. Previously we have discussed simple arrays in Java. In this tutorial, you will learn how to use ArrayList in Java.
In normal routine of Java, the arrays are of fixed size and we have to define the size before using an array. While we implement them once we create an array with some size we have no option to resize it so you must have future knowledge that what is the expected length and size of the array.
Advantages of ArrayList
As we stated in the previous tutorial about standard array, they have fixed size, but when in java we use ArrayList it will enable us to add or remove element with the reduction of array size this means that when you add an element in array, the size of array automatically increases and when you remove an element the size will decrease.
Constructors in ArrayList
In Java array list there are three possible types of the constructors.
- ArrayList()
- ArrayList(Collection object)
- ArrayList(int size)
ArrayList( ) Simple constructor with no parameters is used to create an empty array.
ArrayList(Collection object) This array list is by default initialize with elements of the given collection.
ArrayList(int size) This constructor creates an array of given size in the parameters.
Creating ArrayList
If you want to create an ArrayList you must create an instance/object of ArrayList class, and define the type of the array in triangular brackets. You can also create an ArrayList without specifying the type of ArrayList.
Syntax
ArrayList<type of list> list name=new ArrayList<type of list>();
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.*; public class MyProject { public static void main(String args[]) { ArrayList<String> arr = new ArrayList<String>(); System.out.println("Initial size of array arr is= " + arr.size());//displaying the size of the array list } } |
Adding Elements in ArrayList
When you want to add elements to an ArrayList add()
method is used which takes parameters of the same type as specified at the time of declaring ArrayList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.*; public class MyProject { public static void main(String args[]) { ArrayList<String> arr = new ArrayList<String>();//Creating an arraylist of type String System.out.println("Initial size of array arr is= " + arr.size()); //Adding elements to an ArrayList arr.add("Usman"); arr.add("Ali"); arr.add("Zara"); arr.add("Hamza"); arr.add("Aneela"); arr.add("Nadia"); arr.add("Muneeb"); System.out.println("Size of array after addinf elements: " + arr.size());//displaying the size of the arraylist } } |
Displaying complete ArrayList
When you want to display the complete ArrayList you can display it through the instance you created of the ArrayList class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.*; public class ArrayListDemo { public static void main(String args[]) { // create an array list ArrayList arr = new ArrayList(); System.out.println("Initial size of array arr is= " + arr.size()); // display the array list System.out.println("This array contain the following data: \n" + arr); } } |
Displaying some specific data of ArrayList
You can also display data at some specific location in ArrayList all you have to know is the location of the data as in the case of Arrays. get()
method is used to get the data at some specific index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.*; public class ArrayListDemo { public static void main(String args[]) { // create an array list ArrayList arr = new ArrayList(); System.out.println("Initial size of array arr is= " + arr.size()); //Adding elements to an ArrayList arr.add("Usman"); arr.add("Ali"); arr.add("Zara"); arr.add("Hamza"); arr.add("Aneela"); arr.add("Nadia"); arr.add("Muneeb"); // display the content at specific location of array list System.out.println("Index 5 contains: \n" + arr.get(4)); } } |
Removing Elements from array
you can also remove elements from an ArrayList, remove()
method is used to remove elements from the ArrayList in Java. You can also pass the index of the value you want to delete in the remove()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.*; public class ArrayListDemo { public static void main(String args[]) { // create an array list ArrayList arr = new ArrayList(); System.out.println("Initial size of array arr is= " + arr.size()); // add elements to the array list arr.add("Usman"); arr.add("Ali"); arr.add("Zara"); arr.add("Hamza"); arr.add("Aneela"); arr.add("Nadia"); arr.add("Muneeb"); System.out.println("Size of array after adding elements: " + arr.size()); System.out.println("This array contain the following data: \n" + arr); arr.remove("Zara"); arr.remove(“Hamza”); System.out.println("Size of array after deletions of two elements: " + arr.size()); System.out.println("Contents of array: " + arr); } } |
Important ArrayList Functions
- Array.add(value);
- Array.add(index,value);
- Array.remove(value);
- Array.remove(index);
- Array.size();
- Array.clear();
- Array.clone();
All the above function are defined by their names and do as their names are like the add function is used to add elements in array and remove is use to remove the size function return the actual size of the array and clear function remove all elements from array the clone function make and return the copy of that array.
Read more about ArrayLists in Java