Java Second Class
Java Second Class
Java Second Class
Array are “object “ that hold the fixed Number of variable of the same types.
int rollNum_3=55:
Int rollNum_100
Roll-No[2]=50;
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
o Random access: We can get any data located at an index position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at
runtime. To solve this problem, collection framework is used in Java which grows automatically.
We can declare, instantiate and initialize the java array together by:
1. int a[]={33,3,4,5};//declaration, instantiation and initialization
For-each Loop for Java Array :- we can also print the java array using for-each loop. The java
for-each loop prints the array elements one by one. it holds an array element in a variable, then
executes the body of the loop.
Multidimensional Array in Java :-
In such cases data stored in row and column based index (Also known as matrix form).
1. dataType[ ][ ] arrayRefVar; (or)
2. dataType [ ][ ]arrayRefVar; (or)
3. dataType arrayRefVar[ ][ ]; (or)
4. dataType [ ]arrayRefVar[ ];
1. int[][] arr=new int[3][3];//3 row and 3 column
1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;
Example of Multidimensional Java Array:-
Copying Of Arraycopy method :- We can Copy an array to another by the arraycopy ( ) method of
system Class.
1. public static void arraycopy(
2. Object src, int srcPos,Object dest, int destPos, int length
3. )
Cloning an Array in Java:- Since, Java array implements the Cloneable interface, we can create the clone
of the Java array. If we create the clone of a single-dimensional array, it creates the deep copy of the
Java array. It means, it will copy the actual value. But, if we create the clone of a multidimensional array,
it creates the shallow copy of the Java array which means it copies the references.