What is an Array?
An array is a fixed-size container that stores multiple values of the same type in contiguous memory. Each element is accessed by its index (starting from 0).
java
int[] numbers = {10, 20, 30, 40, 50};
// Index: 0 1 2 3 4
System.out.println(numbers[0]); // 10
System.out.println(numbers[4]); // 50Declaring and Initializing Arrays
java
// Method 1: Declare and initialize with values
int[] scores = {85, 92, 78, 95, 88};
// Method 2: Declare with size, then assign
int[] marks = new int[5];
marks[0] = 85;
marks[1] = 92;
// Method 3: Declare, then create
int[] grades;
grades = new int[]{90, 85, 78};Array Properties
java
int[] arr = {10, 20, 30, 40, 50};
System.out.println(arr.length); // 5 (number of elements)
System.out.println(arr[0]); // 10 (first element)
System.out.println(arr[arr.length - 1]); // 50 (last element)Note: Array length is fixed after creation. You cannot add or remove elements. Use ArrayList for dynamic sizing.
Iterating Over Arrays
java
String[] fruits = {"Apple", "Mango", "Banana", "Grapes"};
// For loop
for (int i = 0; i < fruits.length; i++) {
System.out.println((i + 1) + ". " + fruits[i]);
}
// For-each loop
for (String fruit : fruits) {
System.out.println(fruit);
}Multi-Dimensional Arrays
2D Array (Matrix)
java
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(matrix[0][0]); // 1
System.out.println(matrix[1][2]); // 6
// Iterate over 2D array
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.printf("%4d", matrix[i][j]);
}
System.out.println();
}Common Array Operations
Sorting
java
import java.util.Arrays;
int[] numbers = {64, 25, 12, 22, 11};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
// Output: [11, 12, 22, 25, 64]Searching
java
int[] sorted = {11, 12, 22, 25, 64};
int index = Arrays.binarySearch(sorted, 22);
System.out.println("Found at index: " + index); // 2Copying
java
int[] original = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(original, original.length);
int[] partial = Arrays.copyOfRange(original, 1, 4); // {2, 3, 4}Practical Example
java
public class StudentMarks {
public static void main(String[] args) {
String[] students = {"Arjun", "Priya", "Rahul", "Sneha", "Vikram"};
int[] marks = {85, 92, 78, 95, 88};
// Find highest and average
int max = marks[0], maxIdx = 0, sum = 0;
for (int i = 0; i < marks.length; i++) {
sum += marks[i];
if (marks[i] > max) {
max = marks[i];
maxIdx = i;
}
}
double avg = (double) sum / marks.length;
System.out.printf("Topper: %s with %d marks%n", students[maxIdx], max);
System.out.printf("Class Average: %.1f%n", avg);
}
}Output:
Topper: Sneha with 95 marks
Class Average: 87.6Summary
- Arrays store fixed-size, same-type elements with zero-based indexing
- Use
array.lengthto get the size; access elements witharray[index] - Java supports multi-dimensional arrays (2D, 3D, etc.)
- Use
java.util.Arraysfor sorting, searching, copying, and printing - Arrays cannot be resized — use
ArrayListfor dynamic collections - ArrayIndexOutOfBoundsException occurs when accessing an invalid index