Sunday, September 27, 2015

Short Notes for Array

There is a second form that may be used to declare an array:

type[ ] var-name;

Here, the square brackets follow the type specifier, and not the name of the array variable.

For example, the following two declarations are equivalent:

int one[] = new int[3];
int[] one = new int[3];

The following declarations are also equivalent:

int one[][] = new int[3][4];
int[][] one = new int[3][4];

This alternative declaration form offers convenience when declaring several arrays at the same time. For example.

int[] one, two, three; // create three arrays

creates three array variables of type int. It is the same as writing

int one[], two[], three[]; // create three arrays

The alternative declaration form is also useful when specifying an array as a return type for a method.

No comments:

Post a Comment

If you have any query please comment here, Will get back to you :)