Java ArrayList
Java Full Stack Developer | Passionate about creating robust web applications. Skilled in Java, Spring Boot, Hibernate, and MySql.
Inroduction:
Java
ArrayListuses the dynamic array for storing the elements.It is like an array but no size limit.
We can add or remove elements at anytime.
It is found in
java.utilpackage.It contains duplicate values
Syntax for ArrayList Declaration
ArrayList<String> a1 = new ArrayList<String>();
- We cannot create an arraylist of premetitive datatypes like int ,flot,char etc. It is recommended to use the wrapper classes.
Generic vs Non-Generic
Java collection framework was Non-generic before JDK 1.5,Since 1.5 it is generic.
ArrayList list = new ArrayList(); //It doesnt works ArrayList<String> a1= new ArrayList<String>();//works fine
ArrayList Example
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample{
public static void main(String[] args){
ArrayList<String> a1=new ArrayList<String>();
a1.add("Java");
a1.add("Is");
a1.add("A");
a1.add("Wonderful");
a1.add("Language");
System.out.println(a1);
}
}
//Output
[Java,Is,A,Wonderful,Language]
We can iterate the ArrayList in so many ways:
By using for loop
By using for-each loop
By using Iterator
Iterating ArrayList using for loop
import java.util.ArrayList;
public class ArrayListIteratingUsingForLoop{
public static void main(String[] args){
ArrayList<String> a2=new ArrayList<String>();
a2.add("Java");
a2.add("Is");
a2.add("Robust");
//Iterating using for loop
for(int i=0;i<a2.size();i++) //for single line statement if we dont keep braces it will works,but when coming to multiple statements we need to keep braces
System.out.println(a1.get(i));
}
}
Output:
java
Is
Robust
From the above program, we iterated the ArrayList using for loop. We used two predefined methods like size() and get().The size() returns the length of the ArrayList and get() method is used to fetch the data from the ArrayList.
Iterating ArrayList using for-each loop
import java.util.ArrayList;
public class ArrayListIterationUsingForEachLoop{
public static void main(String[] args){
ArrayList<Integer> a3=new ArrayList<Integer>();
a3.add(155);
a3.add(156);
a3.add(157);
a3.add(156);
a3.add(159);
//Iterating using for each loop
for(Integer i1:a3){
System.out.println(i1);
}
}
}
Output:
155
156
157
156
159
From the above program, we gave the 156 value two times because the ArrayList stores the duplicate values
Iterating ArrayList using Iterator
import java.util.ArrayList;
public class ArrayListIterationUsingIterator{
public static void main(String[] args){
ArrayList<String> a4 =new ArrayList<String>();
a4.add("Java");
a4.add("Is");
a4.add("Portable");
//Iterating using iterator
Iterator it = a4.iterator();
while(it.hasNext()){ //it checks for the elements
System.out.println(it.next()); //prints and goes to next element
{
}
}
Output:
java
Is
Portable
Methods in ArrayList
We are having so many predefined methods in the collections but we are using the most frequently used methods
| Methods | Description |
boolean add(E e) | It is used to append the elements at the end of the list |
E get(int index) | It is used to fetch the element by the specified position |
E set(int index,E e) | It is used to set the element in the specified position |
int size() | It is used to return the number of elements present in the ArrayList |
E remove(int index) | It is used to remove the element in the ArrayList by specified index |
Creating User-defined class objects in ArrayList
class Student{
int id;
String name;
int rollNumber;
Student(int id,String name,int rollNumber){
this.id=id;
this.name=name;
this.rollNumber=rollNumber;
}
import java.util.ArrayList;
import java.util.Scanner;
public class TestArrayList{
public static void main(String[] args){
ArrayList<Student> a5=new ArrayList<Student>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.of students:");
int n =sc.nextInt();
for(int i=1;i<=n;i++){
System.out.println("Enter Student Id:");
int id=sc.nextInt();
System.out.println("Enter Student Name:");
String name=sc.nextline();
System.out.println("Enter Student Roll Number:");
int roll=sc.nextInt();
Student student1= new Student(id,name,roll);
a5.add(student1);
}
System.out.printn("The Students are:");
for(Student s:a5){
System.out.println(s.id+" "+s.name+" "+s.rollNumber);
}
}
}
From the above user defined class ,we are taking the input and passing to the constructor
output:
Enter the no.of student:
2
Enter Student Id:
101
Enter Student Name:
Suresh
Enter Student Roll Number:
1234
Enter Student Id:
102
Enter Student Name:
Ramesh
Enter Student Roll Number:
12345
The Students are:
101 suresh 1234
102 Ramesh 123456
Conclusion:
In conclusion, the Java ArrayList is a versatile and dynamic data structure that offers flexibility in managing collections of objects. Unlike traditional arrays, ArrayList does not have a fixed size, allowing elements to be added or removed as needed. It supports duplicate values and provides various methods for manipulating and accessing elements efficiently. With the introduction of generics in JDK 1.5, ArrayList has become even more robust, ensuring type safety and reducing runtime errors. Whether iterating through elements using loops or iterators, ArrayList proves to be an essential tool in Java programming, offering both simplicity and power in handling dynamic collections.