Arrays:
In Java arrays are dynamically allocated. All array indexes starts from 0. Java has run time array boundary check.
One Dimensional Array:
In Java Arrays are declared in two steps:
a. Declare a array.
Syntax:
type variableName[]; e.g. int rollNos[];
b. Allocate memory for the array.
Syntax:
variableName = new type[size];
e.g. rollNos = new int[30];
Above two steps can be combined in one step as:
e.g. int rollNos[] = new int[30];
Initializing array:
Syntax:
variableName[index] = value; e.g. rollNos[0] = 1;
Arrays can be initialized when they are declared as:
Syntax:
type variableName[] = {initializer list};
e.g. int rollNos[] = {1,2,3,4,5};
Multi Dimensional Array:
Multidimensional arrays are actually arrays of arrays
e,g, int twoD[][] = new int[2][3];
e.g. int twoD[][] = { {2,3,4,5}, {2,2,1,4}, {3,2,4,5} };
Post a Comment
Post a Comment
Please do not enter any spam link in the comment box