An array stores multiple values of the same type in a fixed-size container. In automation, arrays hold test data, element lists, expected values, and configuration sets.
public class ArrayBasics {
public static void main(String[] args) {
// Declare and initialize
String[] browsers = {"Chrome", "Firefox", "Safari", "Edge"};
// Access by index (0-based)
System.out.println(browsers[0]); // Chrome
System.out.println(browsers[3]); // Edge
// Length
System.out.println("Total: " + browsers.length); // 4
// Modify an element
browsers[2] = "Brave";
System.out.println(browsers[2]); // Brave
}
}public class ArrayDeclarations {
public static void main(String[] args) {
// Method 1: Declare with values
int[] scores = {85, 92, 78, 96, 88};
// Method 2: Declare with size, fill later
String[] testNames = new String[3];
testNames[0] = "Login Test";
testNames[1] = "Search Test";
testNames[2] = "Checkout Test";
// Method 3: Declare and initialize with new
double[] prices = new double[]{29.99, 49.99, 99.99};
// Default values for empty arrays
int[] nums = new int[3]; // {0, 0, 0}
boolean[] flags = new boolean[3]; // {false, false, false}
String[] names = new String[3]; // {null, null, null}
System.out.println(nums[0]); // 0
System.out.println(flags[0]); // false
System.out.println(names[0]); // null
}
}Accessing an index outside the array bounds throws ArrayIndexOutOfBoundsException at runtime. If an array has length 5, valid indices are 0-4. Index 5 crashes the program.
import java.util.Arrays;
public class ArrayIteration {
public static void main(String[] args) {
int[] scores = {85, 92, 78, 96, 88};
// for loop
for (int i = 0; i < scores.length; i++) {
System.out.println("Test " + (i + 1) + ": " + scores[i]);
}
// for-each
int sum = 0;
for (int score : scores) {
sum += score;
}
System.out.println("Average: " + (double) sum / scores.length);
// Print entire array
System.out.println(Arrays.toString(scores)); // [85, 92, 78, 96, 88]
}
}import java.util.Arrays;
public class ArrayOps {
public static void main(String[] args) {
int[] scores = {85, 92, 78, 96, 88};
// Sort
Arrays.sort(scores);
System.out.println(Arrays.toString(scores)); // [78, 85, 88, 92, 96]
// Find max and min (after sorting)
System.out.println("Min: " + scores[0]); // 78
System.out.println("Max: " + scores[scores.length - 1]); // 96
// Search (array must be sorted)
int index = Arrays.binarySearch(scores, 88);
System.out.println("88 found at index: " + index); // 2
// Copy
int[] copy = Arrays.copyOf(scores, scores.length);
System.out.println(Arrays.toString(copy));
// Fill
int[] zeros = new int[5];
Arrays.fill(zeros, -1);
System.out.println(Arrays.toString(zeros)); // [-1, -1, -1, -1, -1]
// Compare
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(Arrays.equals(a, b)); // true
System.out.println(a == b); // false — different objects
}
}public class FindMax {
public static void main(String[] args) {
int[] responseTimes = {230, 150, 890, 420, 310};
int max = responseTimes[0];
for (int time : responseTimes) {
if (time > max) {
max = time;
}
}
System.out.println("Slowest response: " + max + "ms"); // 890ms
}
}Q: What is the difference between an array and ArrayList?
A: Arrays have a fixed size — once created, you cannot add or remove elements. ArrayList is resizable — it grows and shrinks dynamically. Arrays can hold primitives (int[]), while ArrayList only holds objects (ArrayList<Integer>). Arrays use .length, ArrayList uses .size(). Use arrays when size is known and fixed; use ArrayList when the size changes at runtime.
Exercise 1: Create an array of 5 test scores. Write code that finds and prints the highest score, lowest score, and average.
Exercise 2: Write a program that reverses an array in-place (without creating a new array). Print the array before and after.
Exercise 3: Given two sorted arrays, merge them into one sorted array. Example: {1, 3, 5} + {2, 4, 6} → {1, 2, 3, 4, 5, 6}.