Open In App

Array Declarations in Java (Single and Multidimensional)

Improve
Improve
Like Article
Like
Save
Share
Report

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; // valid declaration
        int b[]; // valid declaration
        int[] c; // valid declaration
    }
}


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)
    {
        // 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 we can use it like this.




import java.io.*;
  
class GFG { 
    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.

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)
    {
        // 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];
    }
}


Printing array :




/* A complete Java program to demonstrate working
   of one dimensional arrays */
class oneDimensionalArray {
  
    public static void main(String args[])
    {
        int[] a; // one dimensional array declaration
        a = new int[3]; // creating array of size 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:




// Java program to demonstrate different ways
// to create two dimensional array.
import java.io.*;
  
class GFG {
    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 you can use it like this.




// Java program to demonstrate multiple declarations
// of array variable
import java.io.*;
  
class GFG {
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; 
    }
}


Creating one dimensional array and two dimensional array without new operator:




/* Java program for one and two dimensional arrays.
   without new operator*/
class oneTwoDimensionalArray {
  
     public static void main(String args[])
    {
        int[] a[] = { { 1, 1, 1 }, { 2, 2, 2 },
                     { 3, 3, 3 } }, b = { 20 };
  
        // print 1D array
        System.out.println(b[0]);
  
        // print 2D array
        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:




/* Java program for one and two dimensional arrays.
   using new operator*/
class oneTwoDimensionalArray {
  
    public static void main(String args[])
    {
        int[] a[], b = { 20 };
        a = new int[3][3];
        b = new int[3];
  
        // print 1D array
        for (int i = 0; i < 3; i++)
            System.out.println(b[i]);
  
        // print 2D array
        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
Previous
Next
Share your thoughts in the comments
Similar Reads