Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array declaration and initialization in Java. Arrays behave differently, when the position of their subscript indices is changed in their declaration

Tags:

java

arrays

The following is the obvious and usual array declaration and initialization in Java.

int r[], s[];       //<------- r=new int[10]; s=new int[10]; 

A very similar case behaves differently, when the position of [] is changed in the declaration statement like as shown below.

int []p, q[];       //<------- p=new int[10]; q=new int[10][10]; 

Please look at the declaration. The position of [] has been changed from r[] to []p. In this case, the array q behaves like an array of arrays of type int (which is completely different from the previous case).

The question: Why is q, in this declaration int []p, q[]; treated as a two dimensional array?


Additional information:

The following syntax looks wonky.

int []a[]; 

This however, complies fine and just behaves like int a[][]; or int [][]a;.

Hence, the following cases are all valid.

int [][]e[][][]; int [][][][][]f[][][][]; 
like image 609
Tiny Avatar asked May 25 '13 18:05

Tiny


People also ask

How arrays are declared and initialized in Java?

We declare an array in Java as we do other variables, by providing a type and name: int[] myArray; To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15};

What are arrays explain how arrays can be declared and initialized in Java give an example?

Java initialize array is basically a term used for initializing an array in Java. We know that an array is a collection of similar types of data. The array is a very important data structure used for solving programming problems. The word element is used for the values stored in different positions of the array.

What are different ways to declare arrays?

There are two ways you can declare and initialize an array in Java. The first is with the new keyword, where you have to initialize the values one by one. The second is by putting the values in curly braces.

What is the correct way to declare an array of integers in Java?

The syntax for declaring an array is: datatype[] arrayName; datatype : The type of Objects that will be stored in the array eg. int , char etc.


2 Answers

Look at JLS on Arrays:

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

and

Brackets are allowed in declarators as a nod to the tradition of C and C++. The general rules for variable declaration, however, permit brackets to appear on both the type and in declarators, so that the local variable declaration:

float[][] f[][], g[][][], h[];  // Yechh! 

is equivalent to the series of declarations:

float[][][][] f; float[][][][][] g; float[][][] h; 

So for example:

int []p, q[]; 

is just

int[] p, q[] 

which is in fact

int p[]; int q[][] 

The rest are all similar.

like image 149
zw324 Avatar answered Oct 07 '22 18:10

zw324


The sane way of declaring a variable is

type name 

So if type is int[], we should write

int[] array 

Never write

int array[] 

it is gibberish (though it's legal)

like image 30
ZhongYu Avatar answered Oct 07 '22 18:10

ZhongYu