Array Declarations in Java (Single and Multidimensional)
In Java, an Array is used to store multiple values of the same type in a single variable. There are two types of arrays in Java:
- Single-dimensional arrays
- Multi-dimensional arrays
In this article, we are going to discuss how to declare and use single and multidimensional arrays in Java.
Single-Dimensional Array
It is a collection of variables of the same type which is used by a common name. In an array, we can access each element with the help of an index.
Declaration of Single-Dimensional Array
The declaration of a single-dimensional array is:
dataType[] arrayName = new dataType[size];
Note: Here, dataType is the type of elements the array will store, arrayName is the name of the array variable, and size is the number of elements the array can hold.
Example:
import java.io.*;
class Geeks {
public static void main(String[] args)
{
int[] a; // valid declaration
int b[]; // valid declaration
int[] c; // valid declaration
}
}
We can write it in any way. Now, if declare the array like below:
Example:
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// invalid declaration -- If we want to assign
// size of array at the declaration time, it
// gives compile time error.
int a[5];
// valid declaration
int b[];
}
}
Now, suppose we want to write multiple declaration of array variable then how we gonna do this.
Example:
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// valid declaration, both arrays are
// one dimensional array.
int a[], b[];
// invalid declaration
int c[], [] d;
// invalid declaration
int[] e, [] f;
}
}
When we are declaring multiple variable of same time at a time, we have to write variable first then declare that variable except first variable declaration. There is no restriction for the first variable.
Note: When we creates an array it is mandatory to pass the size of array, otherwise we will get compile time error. We can use new operator for creating an array.
Example:
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// invalid, here size of array is not given
int[] a = new int[];
// valid, here creating 'b' array of size 5
int[] b = new int[5];
// valid
int[] c = new int[0];
// gives runtime error
int[] d = new int[-1];
}
}
Implementation of Single Dimensional Array
// A complete Java program to
// demonstrate working of
// one dimensional array
class Geeks {
public static void main(String args[])
{
// one dimensional array declaration
int[] a;
// creating array of size 3
a = new int[3];
for (int i = 0; i < 3; i++) {
a[i] = 100;
System.out.println(a[i]);
}
}
}
Output
100 100 100
Now, we will discuss about Multidimensional Array in detail.
Multi-Dimensional Array
Suppose, we want to create mutli dimensional array of int data type.
Declaration of Mutli-Dimensional Array
The declaration of multidimensional array is:
dataType[][] arrayName = new dataType[rows][columns];
Note: Here, row is the number of rows and coloumn is the number of element in each row.
So, there are multiple ways to declare multidimensional array which are below with examples:
Example:
import java.io.*;
class Geeks {
public static void main(String[] args)
{
int a[][]; // valid
int[][] b; // valid
int[][] c; // valid
int[] d[]; // valid
int[][] e; // valid
int[] f[]; // valid
[][] int g; // invalid
[] int[] h; // invalid
}
}
Now, suppose we want to write multiple declarations of array variable then we can use it like this.
Example:
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// Here, 'a' is two dimensional array, 'b'
// is two dimensional array
int[] a[], b[];
// Here, 'c' is two dimensional array, 'd'
// is two dimensional array
int[] c[], d[];
// Here, 'e' is two dimensional array, 'f'
// is three dimensional array
int[][] e, f[];
// Here, 'g' is two dimensional array,
// 'h' is one dimensional array
int[] g[], h;
}
}
Implementation of Mutli-Dimensional Array
// A complete Java program to
// demonstrate working of
// two-dimensional array
class Geeks {
public static void main(String args[]) {
// Two-dimensional array declaration
int[][] arr;
// Creating a 2x3 matrix (2 rows and 3 columns)
arr = new int[2][3];
// Initializing and printing elements of the matrix
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = 100; // Assigning a value
System.out.println("Element at position (" + i + "," + j + "): " + arr[i][j]);
}
}
}
}
Output
Element at position (0,0): 100 Element at position (0,1): 100 Element at position (0,2): 100 Element at position (1,0): 100 Element at position (1,1): 100 Element at position (1,2): 100
Now, we are going to disucuss one more interesting thing that how to create one dimensional and two dimensional array without the help of new operator.
Example:
// creating one and two dimensional
// array without new operator
class Geeks {
public static void main(String args[]) {
int[] a[] = { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } };
int[] b = { 20 };
// print 1D array
System.out.println(b[0]);
// print 2D array with updated values
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Modifying the element
a[i][j] = 100;
// Print in a single line
System.out.print(a[i][j] + " ");
}
// Move to the next line after each row
System.out.println();
}
}
}
Output
20 100 100 100 100 100 100 100 100 100
Now, let's discuss, how to create one dimensional array and two dimensional array using new operator.
Example:
class Geeks {
public static void main(String args[]) {
// Creating a 2D array
int[][] a;
// Declare a 1D array
int[] b;
// Initialize arrays using the new operator
a = new int[3][3];
b = new int[3];
// print 1D array
for (int i = 0; i < 3; i++) {
// Assign values to b[]
b[i] = 20;
System.out.println(b[i]);
}
// print 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Assign values to a[][]
a[i][j] = 100;
// Print elements on the same line
System.out.print(a[i][j] + " ");
}
// Move to the next line after each row
System.out.println();
}
}
}
Output
20 20 20 100 100 100 100 100 100 100 100 100