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 followsint [] x = {1, 2, 3, 4};
char [] c = {‘a’, ‘b’, ‘c’};
char [] c = {‘a’, ‘b’, ‘c’};
The length of an array
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; }
Subscribe to:
Post Comments (Atom)
0 comments:
Please give your valuable comments.