Java Array Operations – Access, Modify, and Traverse Arrays
Arrays are fundamental in Java for storing multiple values in a single variable. To make the most of arrays, you need to access, modify, and traverse them efficiently.
1. Accessing Array Elements
- Each element in an array can be accessed using its index.
- Index starts from 0.
Example:
public class AccessArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("First element: " + numbers[0]); // 10
System.out.println("Third element: " + numbers[2]); // 30
}
}
⚠️ Accessing an index outside the array bounds throws ArrayIndexOutOfBoundsException.
2. Modifying Array Elements
You can update an array element by assigning a new value to a specific index.
Example:
public class ModifyArray {
public static void main(String[] args) {
int[] scores = {90, 80, 70, 60};
scores[2] = 75; // Change the third element from 70 to 75
for(int i = 0; i < scores.length; i++) {
System.out.println("Score " + i + ": " + scores[i]);
}
}
}
Output:
Score 0: 90
Score 1: 80
Score 2: 75
Score 3: 60
3. Traversing Arrays
Traversing means visiting each element of an array to process it. There are multiple ways to traverse arrays:

a) Using a for loop
int[] numbers = {10, 20, 30, 40, 50};
for(int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
b) Using an enhanced for-each loop
for(int num : numbers) {
System.out.println(num);
}
The enhanced for loop is simpler and more readable, especially for read-only operations.
c) Using Arrays.toString() (for quick printing)
import java.util.Arrays;
System.out.println(Arrays.toString(numbers)); // [10, 20, 30, 40, 50]
4. Common Array Operations
Length of Array
System.out.println("Array length: " + numbers.length); // 5
Sum of Elements
int sum = 0;
for(int num : numbers) {
sum += num;
}
System.out.println("Sum: " + sum); // 150
Finding Maximum and Minimum
int max = numbers[0];
int min = numbers[0];
for(int num : numbers) {
if(num > max) max = num;
if(num < min) min = num;
}
System.out.println("Max: " + max + ", Min: " + min); // Max: 50, Min: 10
Searching for an Element
int target = 30;
boolean found = false;
for(int num : numbers) {
if(num == target) {
found = true;
break;
}
}
System.out.println(found ? "Found" : "Not Found"); // Found
5. Edge Cases
a) Empty Array
int[] empty = {};
System.out.println("Length: " + empty.length); // 0
// Accessing any index will throw ArrayIndexOutOfBoundsException
b) Single-element Array
int[] single = {100};
System.out.println(single[0]); // 100
c) Array with Duplicate Values
int[] duplicates = {1, 2, 2, 3};
System.out.println(Arrays.toString(duplicates)); // [1, 2, 2, 3]
d) Object Array with Null Values
String[] names = {"Alice", null, "Bob"};
for(String name : names) {
System.out.println(name);
}
e) Invalid Index
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // ArrayIndexOutOfBoundsExceptionPoints to Remember
- Access array elements using indices starting from
0. - Modify elements by assigning new values to specific indices.
- Traverse arrays using for loop, enhanced for-each loop, or
Arrays.toString(). - Array length can be obtained using
array.length. - Always check array bounds to avoid
ArrayIndexOutOfBoundsException. - Use loops for operations like sum, max, min, and search.
