Java Array Examples – Practical Examples for Beginners
Arrays are one of the core building blocks in Java. Understanding practical examples helps you master array operations quickly.
1. Example: Printing Array Elements
Printing all elements of an array is one of the most common operations. You can use a for loop or enhanced for-each loop.
Using For Loop
public class PrintArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("Printing using for loop:");
for(int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
Using For-Each Loop
System.out.println("Printing using for-each loop:");
for(int num : numbers) {
System.out.println(num);
}
Output:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
10
20
30
40
50
2. Example: Finding Maximum and Minimum in an Array
Finding the largest and smallest element is a common array task.
Example
public class MaxMinArray {
public static void main(String[] args) {
int[] numbers = {10, 45, 32, 67, 21};
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("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
}
Output:
Maximum value: 67
Minimum value: 10
3. Example: Summing Array Elements
Summing elements is a basic but important operation, often used in real-world programs.
Example
public class SumArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0;
for(int num : numbers) {
sum += num;
}
System.out.println("Sum of array elements: " + sum);
}
}
Output:
Sum of array elements: 150
🏆 Top 5 Interview Questions – Array Operations
1. How do you print all elements of an array in Java?
Answer:
- Using for loop:
int[] arr = {1, 2, 3, 4};
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
- Using enhanced for-each loop:
for(int num : arr) {
System.out.println(num);
}
- Using Arrays.toString():
import java.util.Arrays;
System.out.println(Arrays.toString(arr)); // Output: [1, 2, 3, 4]
2. How do you find the maximum and minimum in an array?
Answer:
int[] arr = {5, 3, 9, 1};
int max = arr[0], min = arr[0];
for(int num : arr) {
if(num > max) max = num;
if(num < min) min = num;
}
System.out.println("Max: " + max); // 9
System.out.println("Min: " + min); // 1
3. How can you calculate the sum of elements in an array?
Answer:
int[] arr = {2, 4, 6};
int sum = 0;
for(int num : arr) {
sum += num;
}
System.out.println("Sum: " + sum); // Output: 12
4. Difference between for loop and enhanced for-each loop
| Feature | For Loop | Enhanced For-each Loop |
|---|---|---|
| Syntax | for(int i=0; i<arr.length; i++) | for(int num : arr) |
| Access | Access by index | Access element value directly |
| Modification | Can modify elements | Cannot modify array elements directly |
| Use Case | Index-based operations | Simple iteration without index |
5. What happens if you try to access an index beyond array length?
Answer: It throws an ArrayIndexOutOfBoundsException.
int[] arr = {1,2,3};
System.out.println(arr[5]); // Exception
