Skip to main content

Arrays in Java

Introduction​

Arrays in Java are used to store multiple values of the same type in a single variable, instead of declaring separate variables for each value. Arrays are a fundamental data structure that can help in organizing and managing data efficiently.

Array Declaration​

Syntax​

To declare an array, specify the data type followed by square brackets and the array name.

dataType[] arrayName;

Example​

int[] numbers;
String[] names;

Array Initialization​

Static Initialization​

You can initialize an array at the time of declaration with a set of values.

int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};

Dynamic Initialization​

You can also allocate memory for the array using the new keyword and then assign values to the array elements.

int[] numbers = new int[5]; // Array of 5 integers
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;

Accessing Array Elements​

Array elements are accessed using their index, which starts from 0.

Example​

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

Looping Through Arrays​

For Loop​

You can use a for loop to iterate through all the elements of an array.

int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

Enhanced For Loop (For-Each Loop)​

The enhanced for loop provides a simpler way to iterate through the elements of an array.

int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}

Multi-Dimensional Arrays​

Java supports multi-dimensional arrays, which are arrays of arrays.

Two-Dimensional Array​

Declaration and Initialization​

int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

Accessing Elements​

System.out.println(matrix[0][0]); // Outputs: 1
System.out.println(matrix[2][2]); // Outputs: 9

Looping Through a Two-Dimensional 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();
}

Example: Matrix Addition​

public class MatrixAddition {
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int[][] matrix2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};

int[][] sum = new int[3][3];

for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result
for (int[] row : sum) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}

Conclusion​

Arrays are a powerful and essential feature in Java, allowing you to store and manage collections of data efficiently. Understanding how to declare, initialize, and manipulate arrays, as well as how to use multi-dimensional arrays, is crucial for effective Java programming.