You are on page 1of 12

Lecture – 12

ArrayList in Java
Afsara Tasneem Misha
Lecturer
Department of CSE
Daffodil International University
Today’s Contents

Java ArrayList

• The ArrayList class is a resizable array, which can be found in the


java.util package.

• The difference between a built-in array and an ArrayList in Java, is


that the size of an array cannot be modified (if you want to add or
remove elements to/from an array, you have to create a new one).
While elements can be added and removed from an ArrayList
whenever you want.
Syntax of Array an ArrayList
• Array
Type [] arrayName = new Type [size]

Ex. int[] arr = new int[10]

• ArrayList
ArrayList<Type> ArrayListName = new ArrayList<Type>();
Ex.
ArrayList<Integer> arrL1 = new ArrayList<>();
ArrayList<String> arrL2 = new ArrayList<>();
ArrayList<Object> arrL3 = new ArrayList<>();
Differences between Array and ArrayList
• An array is basic functionality provided by Java. ArrayList is part of collection
framework in Java.

• Therefore array members are accessed using [], while ArrayList has a set of
methods to access elements and modify them.

• Array is a fixed size data structure while ArrayList is Dynamic. One need not to
mention the size of Arraylist while creating its object.

• Even if we specify some initial capacity, we can add more elements.

• You need to import java.util.ArrayList in you program // import the ArrayList


class
Example of Array and ArrayList
class Test class Test
{ {
public static void main(String args[])
public static void main(String[] args)
{
{
/* ........... Normal Array............. */ /*............ArrayList..............*/
// Create an arrayList with initial capacity 2
int[] arr = new int[2];
ArrayList<Integer> arrL = new
arr[0] = 1; ArrayList<Integer>(2);
arr[1] = 2;
// Add elements to ArrayList
arrL.add(1);
// Access elements of Array arrL.add(2);
System.out.println(arr[0]);
} // Access elements of ArrayList
System.out.println(arrL.get(0));
}
}
}
Example: ArrayList of String
import java.util.ArrayList; Output:

public class MyClass { [Volvo, BMW, Ford, Mazda]


public static void main(String[] args) {

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

cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

System.out.println(cars);
}
}
Built-in Methods for ArrayList
• To add elements to the ArrayListuse use the add() method cars.add("Volvo");
• cars.add("BMW");

• To access an element in the ArrayList, use the get() method and refer to the index number:
• cars.get(0);

• To modify/change an element, use the set() method and refer to the index number:
• cars.set(0, “Alien");

• To remove an element, use the remove() method and refer to the index number:
• cars.remove(0);

• To remove all the elements in the ArrayList, use the clear() method:
• cars.clear();

• To find out how many elements an ArrayList have, use the size method:
• cars.size();
Loop Through an ArrayList
public class MyClass {
Output:
public static void main(String[] args) {
Volvo
ArrayList<String> cars = new ArrayList<String>(); BMW
Ford

cars.add("Volvo"); Mazda

cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

for (int i = 0; i < cars.size(); i++) {


System.out.println(cars.get(i));
}
}
For-each Loop Through an ArrayList
public class MyClass {
Output:

public static void main(String[] args) {


Volvo
BMW
ArrayList<String> cars = new ArrayList<String>();
Ford
Mazda
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

for (String i : cars) {


System.out.println(i);
}

}
}
Sort an ArrayList
import java.util.ArrayList;
import java.util.Collections; // Import the Collections class Output:

BMW
public class MyClass {
Ford
public static void main(String[] args) {
Mazda
ArrayList<String> cars = new ArrayList<String>(); Volvo

cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

Collections.sort(cars); // Sort cars


for (String i : cars) {
System.out.println(i);
}
}
}
Exercises of ArrayList

1. Create an Array List of Integer Data type, Show All the Basic Person
Functions (Add, get, set, remove, clear size)
- name: String
- age: int
2. Consider the Following UML. Now Convert the UML into JAVA
code.
▪ Create ArrayList of Persons.
+Person(String, int)
▪ Add Information of them
+display(): void
▪ Display their Information from ArrayList
+main(String[] ) : void
Thank You

You might also like