ArrayList is a resizable array from the java.util package. In test automation, you use ArrayList to collect dynamic data — element texts, error messages, test results — when you do not know the count beforehand.
import java.util.ArrayList;
public class ArrayListBasics {
public static void main(String[] args) {
ArrayList<String> testResults = new ArrayList<>();
// Add elements
testResults.add("PASS");
testResults.add("FAIL");
testResults.add("PASS");
testResults.add("SKIP");
// Access by index
System.out.println(testResults.get(0)); // PASS
// Size
System.out.println("Total: " + testResults.size()); // 4
// Check contents
System.out.println(testResults.contains("FAIL")); // true
// Remove
testResults.remove("SKIP");
System.out.println(testResults); // [PASS, FAIL, PASS]
// Update
testResults.set(1, "PASS");
System.out.println(testResults); // [PASS, PASS, PASS]
}
}| Method | Returns | Description |
|---|---|---|
| add(element) | void | Adds to end |
| add(index, element) | void | Inserts at position |
| get(index) | Element | Returns element at index |
| set(index, element) | Element | Replaces element, returns old |
| remove(index) | Element | Removes by index, returns removed |
| remove(object) | boolean | Removes first occurrence |
| size() | int | Number of elements |
| contains(object) | boolean | Checks if element exists |
| indexOf(object) | int | First index of element (-1 if absent) |
| isEmpty() | boolean | True if no elements |
| clear() | void | Removes all elements |
import java.util.ArrayList;
public class ArrayListIterate {
public static void main(String[] args) {
ArrayList<String> browsers = new ArrayList<>();
browsers.add("Chrome");
browsers.add("Firefox");
browsers.add("Edge");
// for-each (most common)
for (String browser : browsers) {
System.out.println("Testing: " + browser);
}
// for loop with index
for (int i = 0; i < browsers.size(); i++) {
System.out.println((i + 1) + ". " + browsers.get(i));
}
}
}ArrayList cannot hold primitives directly. Use wrapper classes: Integer for int, Double for double, Boolean for boolean. Java auto-boxes between them.
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListNumbers {
public static void main(String[] args) {
ArrayList<Integer> responseTimes = new ArrayList<>();
responseTimes.add(230); // autoboxing: int → Integer
responseTimes.add(150);
responseTimes.add(890);
responseTimes.add(420);
// Sort
Collections.sort(responseTimes);
System.out.println(responseTimes); // [150, 230, 420, 890]
// Min and max
System.out.println("Fastest: " + Collections.min(responseTimes) + "ms");
System.out.println("Slowest: " + Collections.max(responseTimes) + "ms");
// Sum
int total = 0;
for (int time : responseTimes) { // unboxing: Integer → int
total += time;
}
System.out.println("Average: " + total / responseTimes.size() + "ms");
}
}import java.util.ArrayList;
public class FilterResults {
public static void main(String[] args) {
String[] allResults = {"PASS", "FAIL", "PASS", "FAIL", "PASS", "SKIP", "FAIL"};
ArrayList<Integer> failedIndices = new ArrayList<>();
for (int i = 0; i < allResults.length; i++) {
if (allResults[i].equals("FAIL")) {
failedIndices.add(i);
}
}
System.out.println("Failed test indices: " + failedIndices); // [1, 3, 6]
System.out.println("Failures: " + failedIndices.size() + "/" + allResults.length);
}
}Q: When should you use ArrayList vs Array?
A: Use arrays when the size is known and fixed (test data sets, configuration values). Use ArrayList when elements are added/removed dynamically during execution (collecting element texts from a page, building a list of failed tests). ArrayList provides convenience methods (add, remove, contains) but has slightly more memory overhead than arrays.
Exercise 1: Create an ArrayList of city names. Add 5 cities, remove one by name, insert one at index 2, and print the final list.
Exercise 2: Write a program that takes an array of integers and returns an ArrayList containing only the unique values (remove duplicates).