3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Create and use ArrayLists in Java.
- Add and remove elements from an ArrayList.
- Access and modify ArrayList elements.
- Iterate through ArrayLists.
- Sort and search ArrayList data.
3.2 Overview
An ArrayList is a dynamic collection in Java that can grow and shrink automatically as elements are added or removed. Unlike arrays, ArrayLists do not require a fixed size during creation.
This lesson introduces learners to practical ArrayList operations used in Java applications.
ArrayLists are important in:
- data storage,
- collections processing,
- enterprise applications,
- and object-oriented programming.
Understanding ArrayLists is important because they are widely used in Java software development.
PA0601 — Create an ArrayList
ArrayLists are part of the Java Collections Framework.
Import Statement
Java
import java.util.ArrayList;
Example ArrayList
Java
ArrayList<String> names = new ArrayList<String>();
Practical Activity
Learners must:
- import ArrayList,
- create ArrayLists,
- and use generic data types correctly.
PA0602 — Add Elements to an ArrayList
Elements can be added dynamically to an ArrayList.
Example
Java
names.add(“Alice”);
names.add(“John”);
names.add(“Peter”);
Practical Activity
Learners must:
- add elements to an ArrayList,
- insert multiple values,
- and display results.
PA0603 — Access ArrayList Elements
ArrayList elements are accessed using indexes.
Example
Java
System.out.println(names.get(0));
Practical Activity
Learners must:
- access ArrayList elements,
- display values,
- and use indexes correctly.
PA0604 — Modify ArrayList Elements
Elements in an ArrayList can be updated using the set() method.
Example
Java
names.set(1, “Sarah”);
Practical Activity
Learners must:
- modify existing elements,
- update values,
- and display updated collections.
PA0605 — Remove Elements from an ArrayList
Elements can be removed from an ArrayList dynamically.
Example
Java
names.remove(0);
Remove All Elements
Java
names.clear();
Practical Activity
Learners must:
- remove individual elements,
- clear collections,
- and display updated results.
PA0606 — Determine ArrayList Size
The size of an ArrayList can be determined using the size() method.
Example
Java
System.out.println(names.size());
Practical Activity
Learners must:
- determine collection sizes,
- display collection counts,
- and interpret results.
PA0607 — Iterate Through an ArrayList
ArrayLists can be processed using loops.
Example for Loop
Java
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
Example Enhanced for Loop
Java
for (String name : names) {
System.out.println(name);
}
Practical Activity
Learners must:
- iterate through ArrayLists,
- use loops correctly,
- and display collection data.
PA0608 — Sort an ArrayList
ArrayLists can be sorted using utility methods.
Import Collections
Java
import java.util.Collections;
Example Sorting
Java
Collections.sort(names);
Practical Activity
Learners must:
- sort ArrayLists,
- display sorted values,
- and compare results.
PA0609 — Search ArrayList Elements
ArrayLists support searching and value checking.
Example
Java
names.contains(“Alice”);
Practical Activity
Learners must:
- search collections,
- check for existing values,
- and display results.
PA0610 — Create Complete ArrayList Program
ArrayLists can be combined into complete Java applications.
Example Program
Java
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names =
new ArrayList<String>();
names.add(“Alice”);
names.add(“John”);
names.add(“Peter”);
Collections.sort(names);
for (String name : names) {
System.out.println(name);
}
}
}
Importance of ArrayLists
ArrayLists support:
- dynamic data storage,
- flexible collections,
- and scalable Java applications.
3.5 Key Notes / Summary
- ArrayLists are dynamic collections in Java.
- ArrayLists automatically resize.
- Elements are added using
add(). - Elements are accessed using
get(). - Elements are modified using
set(). - Elements are removed using
remove(). - Collections.sort() sorts ArrayList elements.