An array in Java is a data structure that stores multiple values of the same type in a single variable. Arrays allow you to store a collection of data in a way that each element can be accessed using an index. The array index starts from 0, meaning the first element is at index 0, the second element at index 1, and so on.
Important Points of Java Array
- Arrays in Java are zero-indexed, meaning the first element is at index 0.
- The size of an array is fixed once it is created and cannot be changed.
- Arrays can be of any data type, including primitive types like int, double, char, or reference types like String or custom objects.
Syntax: 1. Declare the Array
type[] arrayName;
OR
type arrayName[];
2. Initialization the Array
Specify the size
arrayName = new type[size];
set the values at the time of declaration
arrayName = {value1, value2, value3, ...};
Example: Declaring and Initializing an Array:
public class ArrayExample {
public static void main(String[] args) {
// Declaring and initializing an array
int[] numbers = new int[2]; // Array of 2 integers
// Initializing array elements
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
// Accessing and printing array elements
System.out.println("Array elements:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output:
2
3
Array with Initialization:
You can also declare and initialize an array in a single step:
public class ArrayExample {
public static void main(String[] args) {
// Declare and initialize the array with values
int[] numbers = {1, 2, 3, 4, 5};
// Access and print the array elements
System.out.println("Array elements:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output:
2
3
4
5
How to update the Array element value
In Java, you can update an element of an array by accessing the specific index and assigning a new value to it. Since arrays are indexed, you can directly assign a new value to any element using its index.
Syntax:
arrayName[index] = newValue;
Explanation:
- arrayName: The name of the array.
- index: The index of the element you want to update (remember that array indices start from 0).
- newValue: The new value you want to assign to the array element.
Example:
public class UpdateArrayExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = {1, 2, 3, 4, 5};
// Print original array
System.out.println("Original array:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
// Update the value at index 1 (second element)
numbers[1] = 20;
// Print updated array
System.out.println("\nUpdated array:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output:
1
2
3
4
5
Updated array:
1
20
3
4
5
Array Length
the length of an array refers to the number of elements it can hold. You can find the length of an array using the length property.
Syntax:
arrayName.length
- arrayName: The name of the array whose length you want to retrieve.
- length: The property that returns the number of elements in the array.
Example: get the array length
public class ArrayLengthExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = {1, 2, 3, 4, 5};
// Get the length of the array
int arrayLength = numbers.length;
// Print the length of the array
System.out.println("The length of the array is: " + arrayLength);
}
}
Output:
Multidimensional Array (2D Array):
Java also supports multidimensional arrays, such as 2D arrays (arrays of arrays).
public class ArrayExample {
public static void main(String[] args) {
// Declaring and initializing a 2D array (3x3 matrix)
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing and printing the elements of the 2D array
System.out.println("Matrix elements:");
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();
}
}
}
Output:
1 2 3
4 5 6
7 8 9