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:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        int[] a; // valid declaration
        int b[]; // valid declaration
        int[] c; // valid declaration
    }
}

chevron_right


We can write it in any way.

Now, if you declare your array like below:

filter_none

edit
close

play_arrow

link
brightness_4
code

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[];
    }
}

chevron_right


Now, suppose we want to write multiple declaration of array variable then we can use it like this.

filter_none

edit
close

play_arrow

link
brightness_4
code

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;
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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];
    }
}

chevron_right


Printing array :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
    }
}

chevron_right


Now, Suppose we want to write multiple declarations of array variable then you can use it like this.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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; 
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

20
100
100
100
100
100
100
100
100
100


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

0
0
0
100
100
100
100
100
100
100
100
100

This article is contributed by Twinkle Patel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.