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
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 program to print 2d array // using Loop method import java.io.*; import java.util.*;
2 min read
Convert an ArrayList of String to a String Array in Java
In Java, as we all know ArrayList class is derived from the List interface. Here we are given an ArrayList of strings and the task is to convert the ArrayList to a string array. Illustration: Input : ArrayList = [ "Geeks", "for", "Geeks" ] Output: String[] = {"Geeks", "for", "Geeks"}Input : ArrayLis
3 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
How to Find Length or Size of an Array in Java?
Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java. In this article, we will learn: How
3 min read
Difference between Array and String in Java
An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
5 min read
ArrayList vs LinkedList in Java
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. However, the limitation of the array is that the size of the array is predefined and fixed. There are multiple ways to solve this problem. In this article, the diff
5 min read
Difference between length of Array and size of ArrayList in Java
Array and ArrayList are two different Entities in Java. In this article we will learn the difference between length of Array and size of ArrayList in Java. Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initializ
2 min read
Java Multi-Dimensional Arrays
Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T
10 min read
One Dimensional Array in Java
An array is a type of Data Structure that can store collections of elements. These elements are stored in contiguous memory locations and the it provides efficient access to each element based on the index of the array element. In this article, we will learn about a one-dimensional array in Java. Wh
7 min read