LESSON 6
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define an ArrayList in Java.
- Create and initialize ArrayLists.
- Add, remove, and update ArrayList elements.
- Access and process ArrayList data using methods.
- Differentiate between arrays and ArrayLists.
3.2 Overview
Java applications often require collections that can grow or shrink dynamically during execution. Unlike arrays, ArrayLists allow developers to store and manage dynamic collections of data without specifying a fixed size.
This lesson introduces learners to ArrayLists and explains how ArrayList methods simplify data management in Java applications. Learners will explore methods used to add, retrieve, update, remove, and process data within ArrayLists.
ArrayLists are commonly used in:
- banking systems,
- inventory systems,
- student systems,
- eCommerce platforms,
- and enterprise applications.
Understanding ArrayLists is important because they form part of the Java Collections Framework and are widely used in modern Java development.
KT0601 — Introduction to ArrayLists
An ArrayList is a resizable collection class found in the Java Collections Framework.
Unlike arrays:
- ArrayLists can grow dynamically,
- elements can be added or removed easily,
- and they provide built-in methods for managing data.
Importing ArrayList
Before using an ArrayList, the ArrayList class must be imported.
import java.util.ArrayList;
Creating an ArrayList
ArrayList<String> names = new ArrayList<String>();
Explanation
| Component | Description |
|---|---|
| ArrayList | Collection class |
| String | Data type stored |
| names | Variable name |
| new | Creates object |
Importance of ArrayLists
ArrayLists help developers:
- manage dynamic data,
- simplify collection processing,
- and avoid fixed-size limitations.
KT0602 — Adding Elements to an ArrayList
The add() method inserts elements into an ArrayList.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Sarah");
names.add("David");
System.out.println(names);
}
}
Output
[John, Sarah, David]
Importance of add()
The add() method allows:
- dynamic data insertion,
- easy collection management,
- and flexible application design.
KT0603 — Accessing ArrayList Elements
The get() method retrieves elements from an ArrayList.
Indexes begin at 0.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Sarah");
System.out.println(names.get(0));
}
}
Output
John
KT0604 — Updating ArrayList Elements
The set() method updates existing elements in an ArrayList.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.set(0, "Peter");
System.out.println(names);
}
}
Output
[Peter]
Importance of set()
The set() method helps:
- modify collection data,
- update application records,
- and maintain dynamic information.
KT0605 — Removing ArrayList Elements
The remove() method deletes elements from an ArrayList.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Sarah");
names.remove(0);
System.out.println(names);
}
}
Output
[Sarah]
Importance of remove()
The remove() method allows applications to:
- delete unnecessary data,
- manage records dynamically,
- and maintain updated collections.
KT0606 — Determining ArrayList Size
The size() method returns the number of elements stored in an ArrayList.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Sarah");
System.out.println(names.size());
}
}
Output
2
Importance of size()
The size() method helps:
- control loops,
- validate data,
- and process collections safely.
KT0607 — Looping Through an ArrayList
Loops are commonly used to process ArrayList elements.
Example Using a for Loop
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Sarah");
for(int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
}
}
Output
John
Sarah
Enhanced for Loop Example
for(String name : names) {
System.out.println(name);
}
KT0608 — Arrays vs ArrayLists
| Arrays | ArrayLists |
|---|---|
| Fixed size | Dynamic size |
| Faster performance | More flexible |
| Stores fixed data | Allows easy insertion/removal |
| Part of core language | Part of Collections Framework |
Choosing Between Arrays and ArrayLists
Arrays are useful when:
- size is fixed,
- performance is critical.
ArrayLists are useful when:
- size changes frequently,
- dynamic data management is needed.
3.5 Key Notes / Summary
- ArrayLists are dynamic collections in Java.
- ArrayLists are part of the Java Collections Framework.
- add() inserts elements into an ArrayList.
- get() retrieves elements.
- set() updates elements.
- remove() deletes elements.
- size() returns the number of elements.
- ArrayLists are more flexible than arrays.