Introduction
Array Lists are part of the List interface in Java. They are very important because of the reason that they can grow or shrink easily depending on the need. So if we do not know the size of our data then it is better to use an Array List. They are not restricted by size.
Array List
- Array List is based on an array but different in the manner that it is resizable accordingly, unlike an array.
- Array List is not synchronized.
- Array List can grow or shrink accordingly.
- Array List is the part of the List interface in Java.
- Array List is synchronized; that is why they are not thread-safe.
- Array List also allows null values.
Array List Methods
There are various methods. We will explain some of them.
Creation of Array List
ArrayList <List_name> = new ArrayList();
Example
- package demo;
- import java.util.*;
- public class Demo {
- public static void main(String args[]) {
- ArrayList ar = new ArrayList();
- System.out.println("ArrayList is : " + ar);
- }
- }
Output
Adding items into Array List
<List_name>.add("item");
Example
- package demo;
- import java.util.*;
- public class Demo {
- public static void main(String args[]) {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- System.out.println("ArrayList is : " + ar);
- }
- }
Output
Finding the size of Array List
<List_name>.size( );
Example
- package demo;
- import java.util.*;
- public class Demo {
- public static void main(String args[]) {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- System.out.println("ArrayList is : " + ar);
- System.out.println("Size of ArrayList is : " + ar.size());
- }
- }
Output
Retrieving item from Array List
<List_name>.get(index);
Example
- package demo;
- import java.util.*;
- public class Demo
- {
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- System.out.println("ArrayList is : " + ar);
- for (int i = 0; i < ar.size(); i++)
- {
- System.out.println("At index : " + i +" item is " + ar.get(i));
- }
- }
- }
Output
Removing item from Array List
<List_name>.remove(index);
Or:
<List_name>.remove("item");
Example
- package demo;
- import java.util.*;
- public class Demo
- {
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- ar.add("sam");
- System.out.println("ArrayList is : " + ar);
- ar.remove(1);
- System.out.println("New ArrayList is " + ar);
- }
- }
Or:
- package demo;
- import java.util.*;
- public class Demo
- {
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- ar.add("sam");
- System.out.println("ArrayList is : " + ar);
- ar.remove("ram");
- System.out.println("New ArrayList is " + ar);
- }
- }
Output
Removing all data from Array List
<List_name>.clear( );
Example
- package demo;
- import java.util.*;
- public class Demo
- {
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- ar.add("sam");
- System.out.println("ArrayList is : " + ar);
- ar.clear();
- System.out.println("ArrayList is : " + ar);
- }
- }
Output
Checking Array List
<List_name>.contains("item");
Example
- package demo;
- import java.util.*;
- public class Demo
- {
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- ar.add("sam");
- System.out.println("ArrayList is : " + ar);
- boolean b = ar.contains("ram");
- System.out.println(b);
- }
- }
Output
Finding an index of an item in Array List
<List_name>.indexOf("item");
Example
- package demo;
- import java.util.*;
- public class Demo
- {
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- ar.add("sam");
- System.out.println("ArrayList is : " + ar);
- System.out.println("Index of ram is : " + ar.indexOf("ram"));
- }
- }
-
-
Output
Checking for empty Array List
if(List_name>.size() == 0)
{
System.out.println("Empty Array List");
}
Example
- package demo;
- import java.util.*;
- public class Demo
-
- {
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- if(ar.size() == 0)
- {
- System.out.println("Empty Array List");
- }
-
- }
- }
Output
Replacing item in Array List
<List_name>.set(index, "new item");
Example
- package demo;
- import java.util.*;
- public class Demo
- {
-
- public static void main(String args[])
- {
- ArrayList ar = new ArrayList();
- ar.add("raj");
- ar.add("ram");
- ar.add("sam");
- System.out.println("ArrayList is : " + ar);
- ar.set(1, "rambo");
-
- System.out.println("ArrayList is : " + ar);
- }
- }
-
Output
Summary
This article explains the Array List in Java and various Array List methods in Java.