All About Array
Array is group of primitive data, group of objects. Array is group of similar data (homogeneous data). Array offers convenient means of grouping related information. Before going ahead we should know some concept about Array: Fixed in size, Indexd based, Index always start from 0, for creating array we must know data type and size of array.
int [ ] x;
above syntax is highly recommended because name is clearly separated by type.
int [6] x;
above syntax is not allowed in java. You can't specify the size of array while array declaration.
2. Multi Dimensional Array
int [ ] [ ] x;
int x[ ] [ ] ;
int [ ] [ ] x;
int [ ] x [ ];
int [ ] x [ ];
All are valid
int [ ] a = new int [3];
In java new keyword is used to create object of class. Here also we are using new keyword. There are predefined java classes for each data type. In above code we are creating array of integer element.
领英推荐
a.getClass().getName();
When we execute above line the the output is: [ I I means Integer.
Array Type Class
int [] [I
double[] [D
short[] [S
byte[] [B
boolean[] [Z
2. 2D Array Creation
In java there is no concept of matrix, instead of this java uses array of arrays.
This concept is used to memory utilization. In matrix some blocks of memory could be wasted.
int [ ] [ ] x = new int [2] [ ]
x[0] = new int [2];
x[1] = new int [3];
x[2] = new int[2];
Sometimes we can declare an array without name such type of nameless arrays are called as annonymous array.
Purpose of Annonymous array: Just for instant use(One time use only)
sum(new int [ ] {10,45,67});
while creating annonymous array we can't specify size of array.
Associate Professor - Computer Science
3 个月More Knowledge…. More Strength