ArrayList in Java
- Badu Blogger

- Jun 30, 2019
- 2 min read
ArrayList is a part of collection framework and is present in java.util package.
It stores dynamically sized collection of elements. It overcomes the drawback of array, that is array's are fixed in size, an ArrayList grows its size automatically when new elements are added to it.
That is, it is much similar to Vector in Java.
ArrayList is part of Java’s collection framework and implements Java’s List interface.
Following are few key points to note about ArrayList in Java -
1. An ArrayList is a re-sizable array, also called a dynamic array. It grows its size to accommodate new elements and shrinks the size when the elements are removed.
2. ArrayList internally uses an array to store the elements. Just like arrays, It allows you to retrieve the elements by their index.
3. Java ArrayList allows duplicate and null values.
4. Java ArrayList is an ordered collection. It maintains the insertion order of the elements.
You cannot create an ArrayList of primitive types like int, char etc. You need to use boxed types like Integer, Character, Boolean etc (so there are Wrapper Classes in Java to convert primitive data types into Classes).
Java ArrayList is not synchronized. If multiple threads try to modify an ArrayList at the same time, then the final outcome will be non-deterministic. You must explicitly synchronize access to an ArrayList if multiple threads are gonna modify it.
Constructors used for creating ArrayList Objects:
a) ArrayList(): This constructor is used to build an empty array list.
b)ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from collection c.
c) ArrayList(int capacity): This constructor is used to build an array list with initial capacity being specified
Creating an ArrayList and adding new elements to it
This example consists of :
a) use of Basic Default Constructor of ArrayList in Java.
b) method add() and how to use it.
import java.util.*;
interface Student
{
void setRoll(int roll);
void setName(String name);
void setAddress(String addr);
int getRoll();
String getName();
String getAddress();
}
class StudentDemo implements Student
{
int roll;
String name,addr;
public StudentDemo()
{
roll=0;
name="NULL";
addr="NULL";
}
public void setRoll(int roll)
{
this.roll=roll;
}
public void setName(String name)
{
this.name=name;
}
public void setAddress(String addr)
{
this.addr=addr;
}
public int getRoll()
{
return roll;
}
public String getName()
{
return name;
}
public String getAddress()
{
return addr;
}
}
public class Collect03
{
public static void main(String args[])
{
int roll;
String addr,name;
ArrayList array = new ArrayList();
Scanner scan = new Scanner(System.in);
System.out.println("Enter Total No. of Students ::");
int n = scan.nextInt();
if(n==0)
{
StudentDemo s2 = new StudentDemo();
array.add(s2);
}
else
{
StudentDemo s1[]= new StudentDemo[n];
for(int i =0;i<n;i++)
{
s1[i] = new StudentDemo();
System.out.println("Enter Roll no. ::");
roll=scan.nextInt();
s1[i].setRoll(roll);
System.out.println("Enter Name ::");
name=scan.next();
s1[i].setName(name);
System.out.println("Enter Address ::");
addr=scan.next();
s1[i].setAddress(addr);
array.add(s1[i]);
}
}
System.out.println("Data is ::");
Iterator itr = array.iterator();
while(itr.hasNext())
{
Student s = (Student)itr.next();
System.out.println("Roll No. ::"+s.getRoll());
System.out.println("Name ::"+s.getName());
System.out.println("Address ::"+s.getAddress());
}
}
}
Output:
Enter Total No. of Students ::
0
Data is ::
Roll No. ::0
Name ::NULL
Address ::NULL
------------------------------------------------
Enter Total No. of Students ::
2
Enter Roll no. ::
1
Enter Name ::
ABC
Enter Address ::
London
Enter Roll no. ::
2
Enter Name ::
PQR
Enter Address ::
NewYork
Data is ::
Roll No. ::1
Name ::ABC
Address ::London
Roll No. ::2
Name ::PQR
Address ::NewYork
Hope you loved the content

Comments