19 October 2010

0 Arrays in Java

array in Java


In Java, all arrays are created dynamically. The operator "new" is used for dynamic memory allocation.
The following statement creates an array of 5 integers.

new int[5]

The above statement returns a reference to the newly created array. References in Java are very similar to pointers in C

Initializing an array in Java
An array can be initialized while it is created as follows

int [] x = {1, 2, 3, 4};
char [] c = {‘a’, ‘b’, ‘c’};

The length of an array

Unlike C, Java checks the boundary of an array while accessing an element in it
Java will not allow the programmer to exceed its boundary
If x is a reference to an array, x.length will give you the length of the array
The for loops can be set up as follows

for(int i = 0; i < x.length; ++i){ x[i] = 5; }


0 comments:

Feeds Comments

Please give your valuable comments.