Array Declarations in Java (Single and Multidimensional)
Arrays in Java | Introduction
One Dimensional Array :
It is a collection of variables of same type which is used by a common name.
Examples:
One dimensional array declaration of variable:
import java.io.*;
class GFG {
public static void main(String[] args)
{
int [] a;
int b[];
int [] c;
}
}
|
We can write it in any way.
Now, if you declare your array like below:
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a[ 5 ];
int b[];
}
}
|
Now, suppose we want to write multiple declaration of array variable then we can use it like this.
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a[], b[];
int c[], [] d;
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.
Now, when we are creating array it is mandatory to pass the size of array; otherwise we will get compile time error.
You can use new operator for creating an array.
import java.io.*;
class GFG {
public static void main(String[] args)
{
int [] a = new int [];
int [] b = new int [ 5 ];
int [] c = new int [ 0 ];
int [] d = new int [- 1 ];
}
}
|
Printing array :
class oneDimensionalArray {
public static void main(String args[])
{
int [] a;
a = new int [ 3 ];
for ( int i = 0 ; i < 3 ; i++) {
a[i] = 100 ;
System.out.println(a[i]);
}
}
}
|
Output:
100
100
100
Two Dimensional Array
Suppose, you want to create two dimensional array of int type data. So you can declare two dimensional array in many of the following ways:
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a[][];
int [][] b;
int [][] c;
int [] d[];
int [][] e;
int [] f[];
[][] int g;
[] int [] h;
}
}
|
Now, Suppose we want to write multiple declarations of array variable then you can use it like this.
import java.io.*;
class GFG {
public static void main(String[] args)
{
int [] a[], b[];
int [] c[], d[];
int [][] e, f[];
int [] g[], h;
}
}
|
Creating one dimensional array and two dimensional array without new operator:
class oneTwoDimensionalArray {
public static void main(String args[])
{
int [] a[] = { { 1 , 1 , 1 }, { 2 , 2 , 2 },
{ 3 , 3 , 3 } }, b = { 20 };
System.out.println(b[ 0 ]);
for ( int i = 0 ; i < 3 ; i++) {
for ( int j = 0 ; j < 3 ; j++) {
a[i][j] = 100 ;
System.out.println(a[i][j]);
}
}
}
}
|
Output:
20
100
100
100
100
100
100
100
100
100
Creating one dimensional array and two dimensional array using new operator:
class oneTwoDimensionalArray {
public static void main(String args[])
{
int [] a[], b = { 20 };
a = new int [ 3 ][ 3 ];
b = new int [ 3 ];
for ( int i = 0 ; i < 3 ; i++)
System.out.println(b[i]);
for ( int i = 0 ; i < 3 ; i++) {
for ( int j = 0 ; j < 3 ; j++) {
a[i][j] = 100 ;
System.out.println(a[i][j]);
}
}
}
}
|
Output:
0
0
0
100
100
100
100
100
100
100
100
100
Last Updated :
21 Aug, 2017
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...