An array is a collection of similar data values with a single name. An array can also be defined as, a special type of variable that holds multiple values of the same data type at a time.
In java, arrays are objects and they are created dynamically using new operator. Every array in java is organized using index values. The index value of an array starts with '0' and ends with 'size-1'. We use the index value to access individual elements of an array.
In java, there are two types of arrays and they are as follows.
- One Dimensional Array
- Multi Dimensional Array
Creating an array
In the java programming language, an array must be created using new operator and with a specific size. The size must be an integer value but not a byte, short, or long. We use the following syntax to create an array.
Syntax
data_type array_name[ ] = new data_type[size];
(or)
data_type[ ] array_name = new data_type[size];
import java.util.Scanner;
public class ArrayExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Declare array size
System.out.print("Enter the number of elements: ");
int size = scanner.nextInt();
// Declare and initialize array
int[] numbers = new int[size];
// Input elements into array
System.out.println("Enter " + size + " integers:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}
// Display array elements
System.out.println("\nThe array elements are:");
for (int i = 0; i < size; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
scanner.close();
}
}
Enter the number of elements: 5
Enter 5 integers:
Element 1: 11
Element 2: 22
Element 3: 33
Element 4: 44
Element 5: 55
The array elements are:
Element at index 0: 11
Element at index 1: 22
Element at index 2: 33
Element at index 3: 44
Element at index 4: 55
Example of Addition of Single Dimension Array
import java.util.Scanner;
public class ArrayAddition {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input size of array
System.out.print("Enter the number of elements: ");
int size = scanner.nextInt();
int[] arr = new int[size];
int sum = 0;
// Input array elements
System.out.println("Enter " + size + " numbers:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
arr[i] = scanner.nextInt();
sum += arr[i]; // Add to sum directly
}
// Display sum
System.out.println("\nSum of array elements: " + sum);
scanner.close();
}
}
Output:
Enter the number of elements: 5
Enter 5 numbers:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
Sum of array elements: 15
Multidimensional Array
In java, we can create an array with multiple dimensions. We can create 2-dimensional, 3-dimensional, or any dimensional array.
In Java, multidimensional arrays are arrays of arrays. To create a multidimensional array variable, specify each additional index using another set of square brackets. We use the following syntax to create two-dimensional array.
Syntax
data_type array_name[ ][ ] = new data_type[rows][columns];
(or)
data_type[ ][ ] array_name = new data_type[rows][columns];
import java.util.Scanner;
public class MatrixAddition {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input number of rows and columns
System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
int cols = scanner.nextInt();
// Declare matrices
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];
int[][] sumMatrix = new int[rows][cols];
// Input first matrix
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("matrix1[" + i + "][" + j + "]: ");
matrix1[i][j] = scanner.nextInt();
}
}
// Input second matrix
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("matrix2[" + i + "][" + j + "]: ");
matrix2[i][j] = scanner.nextInt();
}
}
// Perform addition
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Display result
System.out.println("\nSum of the two matrices:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(sumMatrix[i][j] + "\t");
}
System.out.println();
}
scanner.close();
}
}
Output:
Enter number of rows: 3
Enter number of columns: 3
Enter elements of first matrix:
matrix1[0][0]: 1
matrix1[0][1]: 2
matrix1[0][2]: 3
matrix1[1][0]: 4
matrix1[1][1]: 5
matrix1[1][2]: 6
matrix1[2][0]: 7
matrix1[2][1]: 8
matrix1[2][2]: 9
Enter elements of second matrix:
matrix2[0][0]: 1
matrix2[0][1]: 2
matrix2[0][2]: 3
matrix2[1][0]: 4
matrix2[1][1]: 5
matrix2[1][2]: 6
matrix2[2][0]: 7
matrix2[2][1]: 8
matrix2[2][2]: 9
Sum of the two matrices:
2 4 6
8 10 12
14 16 18