ArrayList
Hey Shouters!! Today we have come up with the ArrayList in Java with good examples to understand. Some main points covered in this article. These are given below:
Introduction
ArrayList Class Declaration
Advantages
Methods
A Simple Example
Synchronization Of ArrayList
Convert ArrayList to LinkedList Program
Introduction
Before discussion of ArrayList some basic concepts are discussed here.
The java.util package contains one of Java's most powerful subsystems i.e. The Collection Framework.
The Collection Framework contains interfaces and classes to provide state-of-the-art technology for managing groups of objects.
Add, Remove, Search etc. activities can be performed using Java collection framework.
The collection framework can contain 4 basic things i.e. Lists, Sets, Maps, Queues.
One interface can extend another interface, similarly, The List interface extends Collection interface to define an ordered collection with duplicates allowed.
The List interface is implemented via ArrayList class.
Java ArrayList class can be used to create containers that store lists of objects.
Java ArrayList can be considered as a growable array.
Java ArrayList gives fast iteration and fast random access.
Java ArrayList implements the new RandomAccess interface.
A very much similar to ArrayList class is Vector class.
Java ArrayList class are imported via This statement
import java.util.ArrayList;
An ArrayList container size can change during program execution.
Java ArrayList is an ordered collection (by index), but not sorted.
Java ArrayList class can contain duplicate elements.
Java ArrayList class is non-synchronized.
ArrayList Class Declaration
ArrayList class is declared as given below,
public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable
To declare an ArrayList using default constructor, You can use this syntax:
ArrayList names = new ArrayList();
To declare an ArrayList using the parameterized constructor, You can use this syntax:
ArrayList names = new ArrayList(int size);
ArrayList names = new ArrayList(Collection c);
You can also specify the capacity/size of the initial ArrayList as well as create ArrayList from other collection types.
Advantages
Some of the advantages ArrayList has over arrays are,
- It can grow dynamically.
- It provides more powerful insertion and search mechanisms than arrays.
Methods
ADD METHOD :
This method is used to adds an item to an ArrayList.
The default version adds an item at the next available location i.e. given below,
public void add(Object)
An overloaded version allows you to specify a position at which to add the item i.e. given below,
public void add(int, Object)
REMOVE METHOD :
This method is used to removes an item from an ArrayList at a specified location i.e. given below,
public void remove(int)
SET METHOD :
This method is used to alters an item at a specified ArrayList location i.e. given below,
public void set(int,Object)
GET METHOD :
This method is used to retrieves an item from a specified location in an ArrayList i.e. given below,
Object get(int)
SIZE METHOD :
This method is used to returns the current ArrayList size i.e. given below,
public int size()
Java program to demonstrate the use of all the above methods describes above.
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String args[])
{
// declaring ArrayList of String Objects
ArrayList<String> myAL = new ArrayList<String>();
// Adding objects to Array list at default index
myAL.add("Apple");
myAL.add("Mango");
myAL.add("Orange");
myAL.add("Grapes");
// Adding objects to Array list at specific index
myAL.add(1, "Orange");
myAL.add(2,"Pineapple");
System.out.println(" Print All the Objects: ");
for (String s: myAL) {
System.out.println(s);
}
System.out.println("Object at index 3 elements from the list: "+myAL.get(3));
System.out.println("Is Chicken is in list: " + myAL.contains("Chicku"));
System.out.println("Size of ArrayList: "+ myAL.size());
myAL.remove("Orange");
System.out.println("New size of ArrayList: "+ myAL.size());
} }
Here we are creating ArrayList named myAL and adding objects using add() methods as well as using index based add method, then printing all the objects using for loop. Then get(), contains(), and size() methods are used, the output is given below :
OUTPUT:
Print All the Objects:
Apple
Orange
Pineapple
Mango
Orange
Grapes
Object at index 3 elements from the list: Mango
Is Chicken is in list: false
Size of ArrayList: 6
New size of ArrayList: 5
A Simple Example
import java.util.*;
// Declare an Student Class
class Student{
int rollno;
String name;
int age;
Student(int rollno, String name, int age)
{
this.rollno = rollno;
this.name = name;
this.age = age;
}
}
class A_L_Example {
public static void main (String args[])
{
Student s1 = new Student(1, "Sachin ", 23);
Student s2 = new Student(2, "Sourav",21);
// creating student class ARRAYLIST
ArrayList<Student> al = new ArrayList<Student>();
// adding Student class object.
al.add(s1);
al.add(s2);
// print
System.out.println("ArrayList of Student values are");
for (Student s: al)
{
System.out.println("Roll: "+s.rollno+ " Name: "+ s.name+ " Age: "+s.age);
}
}
}
Here we are creating an Student class and A_L_Example class. Declare some Student class objects and creating and ArrayList of Student class named al and adding objects using add() methods then printing all the objects using for loop. The output is given below :
OUTPUT:
ArrayList of Student values are
Roll: 1 Name: Sachin Age: 23
Roll: 2 Name: Sourav Age: 21
Synchronization Of ArrayList
Implementation of arrayList is not synchronized is by default. It must synchronized externally using Collections.synchronizedList() method.
import java.util.*;
class Sy_Ar_L
{
public static void main(String args[])
{
List <String> my_l = Collections.synchronizedList(new ArrayList<String>());
my_l.add("LIST");
my_l.add("ARRAY_LIST");
my_l.add("LINKED_LIST");
for(String s:my_l)
{
System.out.println(s);
}
}
}
Output:
LIST
ARRAY_LIST
LINKED_LIST
Convert ArrayList to LinkedList Program
import java.util.ArrayList;
import java.util.LinkedList;
// import classes
class AL_LL
{
public static void main(String args[])
{
ArrayList al_obj = new ArrayList<>();
al_obj.add("Array_LIST");
al_obj.add("Conversion");
al_obj.add("Linked_LIST");
// convertion of ArrayList to LinkedList
LinkedList<String> ll_obj = new LinkedList<>(al_obj);
System.out.println(ll_obj);
} }
Output:
[Array_LIST, Conversion, Linked_LIST]
ArrayList should be used in an application when we need to search objects from the list based on the index.
ArrayList performance degrades when there is lots of insert and update operation in the middle of the list.
ArrayList and Vector are similar classes only difference is Vector has all method synchronized.
To read more :
Comments
Post a Comment