Arrays in Java are used to store multiple values in a single variable, rather than declaring separate variables for each value. They allow you to store homogeneous (same type) data elements, making it easier to manage large amounts of data.


🧭 What is an Array?

An array is a collection of similar data elements stored at contiguous memory locations. You can access array elements using indices.

java
int[] numbers = {10, 20, 30, 40, 50}; System.out.println(numbers[2]); // Output: 30

πŸ—‚️ Types of Arrays in Java

  1. Single-dimensional array – A simple list of values.

  2. Multi-dimensional array – An array of arrays, like a matrix.


1️⃣ Single-Dimensional Array

A single-dimensional array is like a list of items.

java
int[] numbers = new int[5]; // Declare an array of size 5 numbers[0] = 10; // Assigning value to the first element numbers[1] = 20; // Assigning value to the second element System.out.println(numbers[0]); // Output: 10

2️⃣ Multi-Dimensional Array

Multi-dimensional arrays are arrays of arrays, and they can represent things like tables or matrices.

Example of 2D Array (Matrix):

java
int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; System.out.println(matrix[1][2]); // Output: 6 (second row, third column)

πŸ§‘‍πŸ’» Array Operations

Accessing Array Elements

Arrays are accessed using indices. The index starts from 0.

java
int[] numbers = {1, 2, 3, 4, 5}; System.out.println(numbers[0]); // Output: 1

Array Length

You can get the length of an array using the length property.

java
int[] numbers = {10, 20, 30}; System.out.println(numbers.length); // Output: 3

Traversing an Array (Looping through the elements)

java
int[] numbers = {10, 20, 30}; for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }

πŸ§ͺ Code Example: Single-Dimensional Array

java
public class ArrayExample { public static void main(String[] args) { int[] ages = {12, 15, 18, 21}; // Accessing elements System.out.println("First Age: " + ages[0]); // Traversing through array for (int i = 0; i < ages.length; i++) { System.out.println("Age: " + ages[i]); } } }

πŸ§ͺ Code Example: Multi-Dimensional Array

java
public class MultiDimensionalArrayExample { public static void main(String[] args) { int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Accessing elements System.out.println("Element at [1][1]: " + matrix[1][1]); // Traversing a 2D array for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }

πŸ“ Array Initialization

Arrays can be initialized in two ways:

  1. Declaration with Initialization:

java
int[] numbers = {1, 2, 3, 4, 5};
  1. Declaration with new Keyword:

java
int[] numbers = new int[5]; // Size 5, elements are initialized to 0 by default

πŸ§‘‍🏫 Array Methods (Useful for advanced users)

  • Arrays.sort(array): Sorts the array in ascending order.

    java
    int[] numbers = {5, 2, 8, 1}; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers)); // Output: [1, 2, 5, 8]
  • Arrays.toString(array): Converts an array into a string representation.

    java
    int[] numbers = {1, 2, 3, 4}; System.out.println(Arrays.toString(numbers)); // Output: [1, 2, 3, 4]

πŸ“Š Summary Table

Array TypeExampleAccess
1D Arrayint[] numbers = {1, 2, 3};numbers[0]
2D Arrayint[][] matrix = {{1,2},{3,4}};matrix[1][1]