Monday 28 April 2014

Java Arrays



Collection of similiar data types.

1) Declaring Arrays

int[] marks;

Also int marks[]; can possible. but its less readable. so we should not use this format.

2) Construction

marks = new int[5];

Array is the object. so we should use "new" keyword for construct the array. The size of the array is mandatory.

int[] marks=new int[5];

3) Initialization:

marks[0]=34;
marks[1]=37;
marks[2]=32;
marks[3]=30;
marks[4]=31;

Array index starts with 0 and ends with indexvalue-1.
We cannot use negative or maximum index value.


4) Initialization of objects

class pen {
pen[] pens=new pen[3];
}

we can create three object for above statement like pens[0],pens[1],pens[2].


5) Array intializer

if we know the values, we can give the value directly.

int[] marks={12,45,23,67}

No comments:

Post a Comment