Hiii… !!! Today I am going to tell you about the use of array in Java..
Array !!
It is a type of data structure,which contains fixed size sequential homogeneous set of elements.
Syntax: (declaration of an array)
Datatype[] arrayname;
Example:
Int[5] n;
int c = 15;
int[] p={1,2,3,4,5};
Creation of an array:
- Actually in Java,array is act as object created by the new method.
Example:
n=new n[5]; \\ allocate memory for the 5 elements in array n
Some points:
- When memory is allocated for an array, the elements are automatically initialized to their default values: zero for all numeric primitive data types, false for boolean variables and null for references.
- The first element in every array is the zeroth element of that array.
- When accessing an array element, if the index is less than zero or greater than or equal to the array length then an ArrayIndexOutOfBoundsException is thrown.
- Arrays have a length data field (read only), specifying the number of elements in the array.
- The length data field can be accessed through the dot operator.
for ( int k=0; k < a.length ; k++ )
- System.out.println( a[ k ] );The bounds of an array are integers between 0 and length – 1…..
Coping of an array …
Elements of an Arrays can be copied by System.arraycopy() method:
Arraycopy() does not allocate memory for the destination array; the memory must already
be allocated.
Example:
int[] p = { 1, 2, 3,4, 5 };
int[] c = new int[ p.length ];
System.arraycopy(p,0,c,0,p.length); //copy array p to array c
Multidimensional array:
Java also support multidimensional array or you call call it as arrays of arrays.
This can be declared by appending the brackets after the array name.
Example:
Int[][] n=new int[5][5]; //integer array 5 x5 elements
Int[][][] three=new int[2][4][5]; //integer array 2x4x5 elements
Now !! You can download the copy of this article in pdf file by just click on below download link..
0 comments:
Post a Comment
Hey thanax alot to comment i will revert you back soon...