Java Second Class

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Array

Array are “object “ that hold the fixed Number of variable of the same types.

“Why Need Array”

Example:- Class-> Student-> 100 Marks -> Store

Option 1 = Need to create 100 variable

Example :- int rollNum_1 =35:

int rollNum_2 =45:

int rollNum_3=55:

Int rollNum_100

In the case of array Declaration we can directly declare into memory

Example:- int roll_No [100];

Roll-No [0] = 35;

Roll_NO [1]= 40;

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.

There are two types of array.

o Single Dimensional Array


o Multidimensional Array

1. Single Dimensional Array Syntax  


2. dataType []arr; (or)  
3. dataType arr[];  

Instantiation of an Array in java :


1. arrayRefVar=new datatype[size];  

Example of java Program


If we not declared array even assign memory it will by default assigned zero .

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).

Syntax to declare Multidimensional Array in Java

1. dataType[ ][ ] arrayRefVar; (or)  
2. dataType [ ][ ]arrayRefVar; (or)  
3. dataType arrayRefVar[ ][ ]; (or)  
4. dataType [ ]arrayRefVar[ ];   

Example to instantiate Multidimensional Array in java

Int [ ] arr = new int [3 ] [3];

Example to initialize Multidimensional Array in java

1. int[][] arr=new int[3][3];//3 row and 3 column  

Example to initialize Multidimensional Array in java

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.

Synatax of ArrayCopy method : -

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.

You might also like