Arrays are used to store multiple values in a single variable. They are especially useful when working with collections of data that need to be processed in loops.
public class ArrayExample {
public static void main(String[] args) {
// Declaring and initializing an array
int[] numbers = {1, 2, 3, 4, 5};
// Accessing array elements
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
This program declares an array numbers and prints each element of the array using a for loop. The length property is used to determine the size of the array.