Array Declarations in Java (Single and Multidimensional)
Last Updated :
28 Apr, 2025
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:
Java
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:
Java
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:
Java
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:
Java
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
Java
// 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]);
}
}
}
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:
Java
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:
Java
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
Java
// 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]);
}
}
}
}
OutputElement 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:
Java
// 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();
}
}
}
Output20
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:
Java
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();
}
}
}
Output20
20
20
100 100 100
100 100 100
100 100 100
Similar Reads
Multidimensional Collections in Java In Java, we have a Collection framework that provides functionality to store a group of objects. This is called a single-dimensional ArrayList where we can have only one element in a row. Geek but what if we want to make a multidimensional ArrayList, for this functionality for which we do have Multi
4 min read
How to Declare an Array in Java? In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif
3 min read
Passing an Array to a Function in Java Passing an array to a function is an easy-to-understand task in Java. In this article, we will check how to pass an array as a method parameter.Caller Function Vs Called FunctionLet function GFG() be called from another function GFGNews(). Here, GFGNews is called the âCaller Functionâ and GFG is cal
2 min read
Simplest and Best method to print a 2D Array in Java Given a 2d array arr in Java, the task is to print the contents of this 2d array. Method 1: Loop method The first thing that comes to mind is to write a nested for loop, and print each element by arr[i][j]. Java // Java program to print 2d array // using Loop method import java.io.*; import java.uti
2 min read
Different Ways To Declare And Initialize 2-D Array in Java An array with more than one dimension is known as a multi-dimensional array. The most commonly used multi-dimensional arrays are 2-D and 3-D arrays. We can say that any higher dimensional array is an array of arrays. A very common example of a 2D Array is Chess Board. A chessboard is a grid containi
5 min read