Arrays in Java are an important data structure, providing a convenient way to store and manipulate a collection of similar data types. They are used to store multiple values in a single variable, making it easier to manage and process large amounts of data.
Table of Contents
1-d array in java
Declaring an 1-d Array
An array in Java is declared by specifying its type, followed by the name of the array and its size within square brackets. The size of an array cannot be changed once it has been declared.
int[] numbers = new int[5];
Initializing an Array
An array can be initialized at the time of declaration by specifying its values within curly braces
int[] numbers = {1, 2, 3, 4, 5};
Accessing Array Elements
Array elements are accessed using their index, which starts from 0 and goes up to the size of the array minus 1. Here’s an example of accessing array elements in Java:
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("The second element is: " + numbers[1]);
Updating Array Elements
Array elements can be updated by assigning a new value to them using their index. Here’s an example of updating array elements in Java:
int[] numbers = {1, 2, 3, 4, 5};
numbers[2] = 8;
System.out.println("The third element is now: " + numbers[2]);
Iterating Over an Array
To iterate over an array, you can use a loop and access each element in the array one by one. The most common loop used for this purpose is the for loop. Here’s an example of using a for loop to iterate over an array in Java:
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
Finding the Length of an Array
The length of an array can be found using the length
property, which returns the number of elements in the array. Here’s an example of finding the length of an array in Java:
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("The length of the array is: " + numbers.length);
1-d Array Code Example
public class OneDimensionalArrayExample {
public static void main(String[] args) {
// Declare and initialize an array of integers
int[] arr = { 2, 4, 6, 8, 10 };
// Accessing elements of the array using index
System.out.println("Element at index 2: " + arr[2]);
// Updating an element of the array using index
arr[3] = 12;
System.out.println("Updated element at index 3: " + arr[3]);
// Iterating over the array using a for loop
System.out.print("Array elements: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
// Finding the length of the array
System.out.println("Length of the array: " + arr.length);
}
}
Output

2-D Arrays in Java
Java also supports two-dimensional arrays, also known as matrix arrays. These arrays are essentially arrays of arrays, providing a convenient way to store and manipulate multi-dimensional data.
Declaring a Two-Dimensional Array
A two-dimensional array in Java is declared by specifying its type, followed by the name of the array and its size within two sets of square brackets. The size of a two-dimensional array cannot be changed once it has been declared. Here’s an example of declaring a two-dimensional array in Java:
int[][] myArray = new int[3][4];
Initializing a Two-Dimensional Array
A two-dimensional array can be initialized at the time of declaration by specifying its values within nested curly braces. Here’s an example of initializing a two-dimensional array in Java:
int[][] matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
Accessing Elements in a Two-Dimensional Array
Elements in a two-dimensional array can be accessed using a combination of row and column indices. The row index comes first, followed by the column index within square brackets. Here’s an example of accessing an element in a two-dimensional array:
int element = matrix[1][2];
Iterating Over a Two-Dimensional Array
To iterate over a two-dimensional array, you can use nested loops, where the outer loop iterates over the rows and the inner loop iterates over the columns. Here’s an example of using nested loops to iterate over 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();
}
2-d Array Code Example
public class TwoDimensionalArrayExample {
public static void main(String[] args) {
// Declare and initialize a 2D array of integers
int[][] arr = { { 2, 3, 5 }, { 7, 11, 13 } };
// Accessing elements of the array using row and column indices
System.out.println("Element at row 1, column 2: " + arr[1][2]);
// Updating an element of the array using row and column indices
arr[0][1] = 4;
System.out.println("Updated element at row 0, column 1: " + arr[0][1]);
// Iterating over the array using nested for loops
System.out.println("Array elements:");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
// Finding the length of the array
System.out.println("Number of rows in the array: " + arr.length);
System.out.println("Number of columns in the array: " + arr[0].length);
}
}
Output

Conclusion
Arrays are essential data structures in Java, used to store and manipulate collections of similar data types. They can be 1-dimensional or multi-dimensional, with elements accessed via indexing. To iterate over an array, for loops can be used.