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
Points 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.
🏆 Top 5 Interview Questions – Array Access & Operations
1. How do you access and modify elements in an array?
Answer:
- Access elements using index:
array[index] - Modify elements by assigning a new value:
array[index] = newValue
int[] numbers = {10, 20, 30};
System.out.println(numbers[1]); // Access: 20
numbers[1] = 50; // Modify
System.out.println(numbers[1]); // Output: 50
2. Difference between for loop and enhanced for-each loop for arrays.
Answer:
| Feature | For Loop | Enhanced For-each Loop |
|---|---|---|
| Syntax | for(int i=0;i<arr.length;i++) | for(int num : arr) |
| Access | Can access element by index | Directly accesses element value |
| Modification | Can modify array elements | Cannot modify array elements directly |
| Use Case | When index is needed | When only iteration is needed |
3. How do you find the length of an array?
Answer: Use the length property of the array:
int[] arr = {1,2,3,4};
System.out.println(arr.length); // Output: 4
4. How can you search for an element in an array?
Answer: Using linear search (iterate each element)
int[] arr = {5, 10, 15};
int key = 10;
boolean found = false;
for(int i = 0; i < arr.length; i++) {
if(arr[i] == key) {
found = true;
break;
}
}
System.out.println(found ? "Found" : "Not Found"); // Output: Found
Or using Arrays.binarySearch() for sorted arrays:
import java.util.Arrays;
int[] arr = {1, 3, 5, 7};
int index = Arrays.binarySearch(arr, 5);
System.out.println(index); // Output: 2
5. What exception occurs when accessing an invalid index?
Answer: Accessing an index outside the array range throws ArrayIndexOutOfBoundsException.
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // Exception
