Arrays
Introduction
An array in Java is a data structure used to store multiple values of the same data type in a single variable. Instead of creating separate variables for each value, arrays allow us to store all values together in a contiguous memory location.
Example
public class Main {
public static void main(String[] args) {
// declaration and initialization of array
int numbers[] = {10, 20, 30, 40, 50};
// accessing array elements
System.out.println("First Element: " + numbers[0]);
System.out.println("Second Element: " + numbers[1]);
System.out.println("Third Element: " + numbers[2]);
// printing all elements using loop
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}