Introduction:
An array is an index collection of a fixed number of homogeneous data elements.
The main advantage of the array is, we can represent a huge number of values using a single variable. So, the readability of the code will be improved.
But the main disadvantage of an array is, fixed in size. i.e. Once we create an array there is no chance of increasing or decreasing the size based on our requirement. Hence, to use the array concept compulsory we should know the size in advance, which may not possible always.
Array Declaration:
There are three ways to declare the one-dimensional array. They are as follows
int[] x;
int []x;
int x[];
All above are valid, but int[] x; is recommended, because variable name clearly separated from the type of array.
At the time of declaration, we can't specify the size, otherwise, we will get the compile-time error.
int[6] x; Invalid
int[] x; Valid
We can declare a two-dimensional array in the following ways.
int[][] x;
int [][]x;
int x[][];
int[] []x;
We can declare more than one array of the same type in the same line.
e.g. int[] a, b;
Que: Which of the following are valid if valid then write the dimension of the arrays.
If we want to specify dimension before variable that facility is applicable only for 1st variable in a declaration.
If we are trying to apply for the next variable, we will get a compile-time error.
e.g.
int[] []a, []b, []c;
Comments
Post a Comment