Java Arrays
What is an Array?
An array is a fixed-size collection of elements of the same type, stored in contiguous memory and accessed by index (starting at 0).
Declaring and Using Arrays
int[] numbers = {10, 20, 30};
System.out.println(numbers[0]); // 10
System.out.println(numbers.length); // 3
Why Testers Use Arrays
Arrays are handy for holding a fixed set of test data (like a list of expected status codes) that a test loops through and verifies.
Ways to Create and Fill an Array
Java gives you a few syntaxes for arrays. Pick the one that matches whether you already know the values.
// Size known, values later. Elements start with default values.
int[] codes = new int[3]; // {0, 0, 0}
String[] names = new String[2]; // {null, null}
boolean[] flags = new boolean[2]; // {false, false}
codes[0] = 200;
codes[1] = 201;
codes[2] = 204;
// Values known up front
String[] browsers = {"chrome", "firefox", "edge"};
The size is fixed the moment the array is created. If you later need room for a fourth browser, you must create a new, larger array or switch to an ArrayList.
Looping Through Test Data
A tester often stores a set of inputs or expected values in an array and checks each one. Both loop styles below visit every element.
int[] expected = {200, 201, 204};
// Index-based loop: useful when you need the position
for (int i = 0; i < expected.length; i++) {
System.out.println("Case " + (i + 1) + " expects " + expected[i]);
}
// Enhanced for loop: simpler when only the value matters
for (int code : expected) {
System.out.println("Checking status " + code);
}
Two-dimensional arrays and TestNG data providers
A 2D array is an array of arrays, like a small table. TestNG data providers return exactly this shape, Object[][], where each inner array becomes one test run:
@DataProvider(name = "loginData")
public Object[][] loginData() {
return new Object[][] {
{"user1@test.com", "Pass@123", true},
{"user1@test.com", "wrong", false},
{"", "Pass@123", false}
};
}
@Test(dataProvider = "loginData")
public void login(String email, String password, boolean shouldPass) {
// perform login and assert the outcome
}
Here the test method runs three times, once per row, with the columns mapped to the method parameters in order.
Helpful Methods in java.util.Arrays
| Method | What it does |
|---|---|
Arrays.toString(arr) | Readable text like [200, 201, 204] for logs |
Arrays.sort(arr) | Sorts the array in place |
Arrays.equals(a, b) | Compares contents element by element |
Arrays.asList(...) | Wraps an object array as a fixed-size list |
Arrays.copyOf(arr, n) | Copies into a new array of length n |
A common test pattern is to read the text of several web elements, sort them, and compare them with an expected sorted array using Arrays.equals. That is how many automation scripts verify that a "sort by name" feature works.
Arrays are still a good choice when the size is fixed and known, such as the columns of a data provider or a set of expected HTTP codes. When results come from the page and their count may change, collect them into a list instead, then convert with list.toArray(new String[0]) only if an API specifically needs an array.
Array Mistakes to Avoid in Test Code
- Going past the last index. Valid indexes run from 0 to
length - 1. Usingi <= arr.lengththrowsArrayIndexOutOfBoundsException. - Comparing arrays with
==. That checks whether both variables point to the same object, not whether the values match. - Printing the array directly.
System.out.println(arr)prints something like[I@1b6d3586; wrap it inArrays.toString. - Using arrays for growing data. Lists of elements found on a page change in size, so
List<WebElement>is the better fit. - Forgetting null defaults. A new
String[]holds nulls until filled, which can cause aNullPointerException.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.