It is a resizable array or a dynamic array. It uses this dynamic array mechanism to store its elements. ArrayList class is found in the java.util package in java.
Hierarchy of ArrayList class
public class ArrayList<E> extends AbstractList<E> implements List<E>,
RandomAccess, Cloneable, Serializable
Features of ArrayList
- An ArrayList allows duplicate elements.
- An ArrayList maintains the insertion order.
- An ArrayList is not synchronized, hence not thread safe.
- An ArrayList allows RandomAccess of elements as it implements the same interface functionality.
- Insertion/deletion operation is a little slow if compared to a linkedList as arrayList does a lot of shifting of elements(being a resizable array).
- The iterators returned by iterator and listIterator methods of ArrayList are fail-fast. Which means, if the list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException.
ArrayList Constructors
- ArrayList() : This is used to build an empty arrayList.
- ArrayList(int capacity) : This is used to build an array list that has the specified initial capacity.
- ArrayList(Collection <? extends E> c) : This is used to build an array list that is initialized with the elements of the collection c .
Coding Examples of HashMap
1. Declare & Add Items to ArrayList
import java.util.ArrayList; // import this class
public class TestArrayList {
public static void main(String[] args) {
ArrayList<String> vehicles = new ArrayList<String>();
vehicles.add("Merc");
vehicles.add("BMW");
vehicles.add("Ford");
vehicles.add("JLR");
System.out.println(vehicles);
}
}
OUTPUT
[Merc, BMW, Ford, JLR]
2. Iterating Collection through the for-each loop
import java.util.*;
class TraverseList{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Alpha");
al.add("Beta");
al.add("Gamma");
//Traversing list using for-each loop
for(String obj:al)
System.out.println(obj);
}
System.out.println("-------------");
// Traversing list using java 8 forEach loop and lambda
al.forEach(x -> { // "x" can be replaced by any variable name
System.out.println(x);
});
}
OUTPUT
Alpha Beta Gamma ------------- Alpha Beta Gamma
3. set(), get(), isEmpty(), remove() methods of ArrayList
public static void main(String[] args) {
List<String> groceryList = new ArrayList<String>();
// Check if an ArrayList is empty
System.out.println("Is the groceryList empty? : " + groceryList.isEmpty());
groceryList.add("Deo");
groceryList.add("Cookies");
groceryList.add("Cereals");
groceryList.add("Bread");
groceryList.add("Cheese");
// Find the size of an ArrayList
System.out.println("Here is the grocery list of " + groceryList.size() +" items");
System.out.println(groceryList);
// Retrieve the element at a given index
String perfume = groceryList.get(0);
String lastItem = groceryList.get(groceryList.size() - 1);
System.out.println("Best perfume: " + perfume);
System.out.println("Last item in the list: " + lastItem);
// Modify the element at a given index
groceryList.set(4, "Amazon");
System.out.println("Modified grocery list: " + groceryList);
//remove the item from the list
groceryList.remove(3);
System.out.println("Grocery list after removing item 3: "+groceryList);
}
OUTPUT
Is the groceryList empty? : true Here is the grocery list of 5 items [Deo, Cookies, Cereals, Bread, Cheese] Best perfume: Deo Last item in the list: Cheese Modified grocery list: [Deo, Cookies, Cereals, Bread, Amazon] Grocery list after removing item 3: [Deo, Cookies, Cereals, Amazon]
4. ArrayList of User defined objects
import java.util.ArrayList;
import java.util.List;
class Employee {
private String name;
private int empId;
public Employee(String name, int empId) {
this.name = name;
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
}
public class TestArraylist {
public static void main(String[] args) {
List<Employee> empList = new ArrayList<>();
empList.add(new Employee("Jason", 30));
empList.add(new Employee("Winston", 34));
empList.add(new Employee("Bourne", 29));
empList.forEach(Employee -> {
System.out.println("Name : " + Employee.getName() +
", Employee Id : " + Employee.getEmpId());
});
}
}
OUTPUT
Name : Jason, Employee Id : 30 Name : Winston, Employee Id : 34 Name : Bourne, Employee Id : 29
Summary
So here we are, we hope now you are confident and know how to use ArrayList in java.
We tried our best to simplify the explanation, if you feel any difference or you need help in understanding any such thing, then feel free to contact us through email and subscribe to ProgrammerToday.