Introduction
An array is a collection of elements of the same type stored in contiguous memory locations. Arrays have fixed size and are indexed starting from 0.
Array Declaration
Syntax:
type[] arrayName;
// OR
type arrayName[];
Examples:
int[] numbers; // Preferred style
String[] names;
double[] prices;
int marks[]; // Alternative style
Array Creation
Method 1: New keyword
int[] numbers = new int[5]; // Array of 5 integers
// Default values: [0, 0, 0, 0, 0]
String[] names = new String[3]; // Array of 3 Strings
// Default values: [null, null, null]
Method 2: Initialization
int[] numbers = {10, 20, 30, 40, 50};
String[] fruits = {"Apple", "Banana", "Orange"};
double[] prices = {99.99, 49.99, 29.99};
Method 3: Declaration + Initialization
int[] numbers;
numbers = new int[]{1, 2, 3, 4, 5};
Accessing Array Elements
Using Index (0-based):
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]); // 10 (first element)
System.out.println(numbers[2]); // 30 (third element)
System.out.println(numbers[4]); // 50 (last element)
// Modify elements
numbers[1] = 25;
System.out.println(numbers[1]); // 25
Array Length
int[] numbers = {10, 20, 30, 40, 50};
int size = numbers.length; // 5 (property, not method)
System.out.println("Array size: " + size);
// Access last element
int last = numbers[numbers.length - 1]; // 50
Traversing Arrays
Using for Loop:
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Using for-each Loop:
int[] numbers = {10, 20, 30, 40, 50};
for (int num : numbers) {
System.out.println(num);
}
Common Array Operations
1. Sum of Elements:
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : numbers) {
sum += num;
}
System.out.println("Sum = " + sum); // 150
2. Find Maximum:
int[] numbers = {45, 23, 67, 12, 89};
int max = numbers[0];
for (int num : numbers) {
if (num > max) {
max = num;
}
}
System.out.println("Maximum = " + max); // 89
3. Find Minimum:
int[] numbers = {45, 23, 67, 12, 89};
int min = numbers[0];
for (int num : numbers) {
if (num < min) {
min = num;
}
}
System.out.println("Minimum = " + min); // 12
4. Search Element:
int[] numbers = {10, 20, 30, 40, 50};
int target = 30;
boolean found = false;
for (int num : numbers) {
if (num == target) {
found = true;
break;
}
}
if (found) {
System.out.println("Found");
} else {
System.out.println("Not found");
}
5. Reverse Array:
int[] numbers = {10, 20, 30, 40, 50};
int n = numbers.length;
for (int i = 0; i < n / 2; i++) {
int temp = numbers[i];
numbers[i] = numbers[n - 1 - i];
numbers[n - 1 - i] = temp;
}
// Result: {50, 40, 30, 20, 10}
Multi-Dimensional Arrays
2D Arrays:
Declaration and Creation:
int[][] matrix = new int[3][4]; // 3 rows, 4 columns
// Initialization
int[][] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Accessing Elements:
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
System.out.println(matrix[2][1]); // 8
// Modify
matrix[1][1] = 50; // Changes 5 to 50
Traversing 2D Array:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Using nested loops
for (int i = 0; i < matrix.length; i++) { // rows
for (int j = 0; j < matrix[i].length; j++) { // columns
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Output:
// 1 2 3
// 4 5 6
// 7 8 9
// Using for-each
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
Arrays Class (java.util.Arrays)
Import:
import java.util.Arrays;
Useful Methods:
1. sort() - Sort array:
int[] numbers = {45, 12, 89, 23, 67};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
// Output: [12, 23, 45, 67, 89]
2. toString() - Print array:
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(numbers));
// Output: [10, 20, 30, 40, 50]
3. fill() - Fill with value:
int[] numbers = new int[5];
Arrays.fill(numbers, 10);
System.out.println(Arrays.toString(numbers));
// Output: [10, 10, 10, 10, 10]
4. equals() - Compare arrays:
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
boolean same = Arrays.equals(arr1, arr2);
System.out.println(same); // true
5. binarySearch() - Search in sorted array:
int[] numbers = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(numbers, 30);
System.out.println("Index: " + index); // 2
6. copyOf() - Copy array:
int[] original = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copy));
// Output: [1, 2, 3, 4, 5]
Array Input from User
import java.util.Scanner;
public class ArrayInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter array size: ");
int n = sc.nextInt();
int[] numbers = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
numbers[i] = sc.nextInt();
}
System.out.println("Array elements:");
for (int num : numbers) {
System.out.print(num + " ");
}
sc.close();
}
}
Common Mistakes
1. ArrayIndexOutOfBoundsException:
int[] arr = {10, 20, 30};
System.out.println(arr[3]); // ❌ Error (index 0-2 only)
System.out.println(arr[-1]); // ❌ Error (negative index)
2. Using = to Compare:
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
System.out.println(arr1 == arr2); // false (different references)
System.out.println(Arrays.equals(arr1, arr2)); // true (same content)
3. Forgetting Length Property:
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr.length); // ✓ 5
System.out.println(arr.length()); // ❌ Error (not a method)
Quick Reference
// Declaration & Creation
int[] arr = new int[5];
int[] arr = {1, 2, 3, 4, 5};
// Access
arr[0] = 10; // Set
int x = arr[0]; // Get
// Length
int size = arr.length;
// Traverse
for (int i = 0; i < arr.length; i++) { }
for (int element : arr) { }
// 2D Array
int[][] matrix = new int[3][4];
int[][] matrix = {{1,2}, {3,4}};
// Arrays class
Arrays.sort(arr);
Arrays.toString(arr);
Arrays.equals(arr1, arr2);
Exam Tips
Remember:
- Array index starts at 0
- Last index is length - 1
- Use length property (not method)
- Size is fixed after creation
- Default values: 0 (int), 0.0 (double), false (boolean), null (objects)
- Use Arrays.sort() to sort
- Use Arrays.toString() to print
- for-each cannot modify array elements
- 2D array: arr[row][column]
- Handle ArrayIndexOutOfBoundsException
Common Questions:
- How to declare and initialize arrays?
- What is array length?
- Difference between for and for-each?
- How to sort array?
- What is 2D array?
- Default values in array?