Java ArrayList

Java ArrayList, create ArrayList, add elements to ArrayList, return ArrayList size, remove elements of ArrayList, and clear all elements of the ArrayList.

java tutorial

Core Java Step by Step Tutorial

Java Array is Static Data Structure and ArrayList is Dynamic Data Structure

Array versus ArrayList in Java

Array is Static (its size is fixed), ArrayList is Dynamic….

Java Array Example:
int [] a= new a[4];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50; //Error

ArrayList is Dynamic and you can add or remove elements….
Note: ArrayList is a predefined class that we have to import from java.util package

Create an ArrayList object called “myTools: that will store strings:

import java.util.ArrayList; // import the ArrayList class

ArrayList<String> myTools = new ArrayList<String>(); // Create an ArrayList object

Add Items

ArrayList <String> myTool = new ArrayList<> ();

myTools.add(“Selenium”);
myTools.add(“UFT”);
myTools.add(“JMeter”);

Access an Item

To access an element in the ArrayList, use the get() method and refer to the index number:

Example
myTools.get(0);

Change an Item

To modify an element, use the set() method and refer to the index number:

Example
myTools.set(0, “Appium”);

Remove an Item

To remove an element, use the remove() method and refer to the index number:

Example:
myTools.remove(1);

Remove all Items

To remove all items, use the clear() method and refer to the index number:

Example:
myTools.remove(1);

ArrayList Size

To find out how many elements an ArrayList have, use the size method:

Example:
myTools.size();

Examples:

1. Create Integer type ArrayList and conduct operations

ArrayList <Integer> abc = new ArrayList <> ();

//Add elements to ArrayList
abc.add(100);
abc.add(200);
abc.add(300);

//Return & Print an Array Element
int val=abc.get(2);
System.out.println(val);//300

//Print an Array Element
System.out.println(abc.get(1));//200

//Return & Print Size of the ArrayList
int x = abc.size();
System.out.println(x);//3

//Print Size of the ArrayList
System.out.println(abc.size());//3

//Remove an element
System.out.println(abc.get(1));//200
abc.remove(1);
System.out.println(abc.get(1));//300

System.out.println(abc.size());//2
abc.add(400);
abc.add(500);
System.out.println(abc.size());//4

//Check the existence of Elements
System.out.println(abc.contains(200));//false
System.out.println(abc.contains(300));//true

//Remove all elements
System.out.println(abc.size());//4
abc.clear();
System.out.println(abc.size());//0
abc.add(123);
abc.add(234);
System.out.println(abc.size());//2

2. Create String type ArrayList

ArrayList <String> myTool = new ArrayList<> ();

myTool.add(“Selenium”);
myTool.add(“UFT”);
myTool.add(“JMeter”);

String val = myTool.get(1);
System.out.println(val);//UFT

int x= myTool.size();
System.out.println(x);//3

boolean y=myTool.contains(“JMeter”);
System.out.println(y);//true

myTool.clear();
System.out.println(myTool.size());//0

3. Create Character type ArrayList

ArrayList <Character> xyz = new ArrayList<> ();

xyz.add(‘A’);
xyz.add(‘3’);
xyz.add(‘&’);

System.out.println(xyz.size());//3

char a= xyz.get(2);
System.out.println(a);//&

Java ArrayList Video

G C Reddy

G C Reddy, Working as a Software Testing Trainer in Hyderabad, India.

You may also like...