Documente Academic
Documente Profesional
Documente Cultură
Arrays in Java
Indexes start at 0.
All data types (primitive and user-defined
objects) can be put into arrays.
Any expression which evaluates to an
integer can be used as the array subscript.
Declaring Arrays
int myarray[];
myarray = new int[100];
Example Declarations
int arr[3][3] = {
{11,12, 23},
{34, 45, 56},
{67, 78, 89}
};
These values are stored row-wise in the
arr which is a double dimensional array.
Therefore the values after initialization
will be –
arr[0][0] = 11
arr[0][1] = 12
arr[0][2] = 23
arr[1][0] = 34
arr[1][1] = 45
arr[1][2] = 56
arr[2][0] = 67
arr[2][1] = 78
arr[2][2] = 89
Values can also be stored at desired
locations during initialization. For example
int arr[3][3] = {
{34},
{23, 45},
{89, 67, 54}
};
This will cause following effect in the arr –
arr[0][0] = 34
arr[0][1] = 0
arr[0][2] = 0
arr[1][0] = 23
arr[1][1] = 45
arr[1][2] = 0
arr[2][0] = 89
arr[2][1] = 67
arr[2][2] = 54
Multi Dimensional Arrays
int arr[][][];
arr[][][] = new int[size1] [size2]
[size3];