Static array:
an array which doesn't change at compilation time is called static array
Example
class student {
/* following array is static and can't be changed */
final int static_array[]= new int [20] ;
public void init (){
static_array [0]= 5;
static_array [1]=6;
/*print two elements from static array */
System.out.println (static_array [0] +static_array [1]);
}
public static void main(String [] args){
student std = new student();
std.init ();
}
};
Result :
11
Explanation :
In the above example we have created an array by name (static_array) .
The size of this array is fixed and cannot be changed at compilation time.
How ever we can assign different values to this array indexes.
When an array is initiallized with a fixed size its mean that it is a static array.
Dynamic array :
An array which changes in compilation time is called dynamic array
Example :
class student{
/* first size of array is 30 */
int dynamic_array [] = new int [30];
public void size1 (){
/* print 1st size */
System.out.println(dynamic_array.length);
}
public void size2(int size){
/* 2nd initialization of dynamic array */
dynamic_array = new int [size] ;
/* print 2nd size */
System.out.println (dynamic_array.length);
}
public static void main (String [] args) {
student std = new student ();
/* call first size method */
std.size1 ();
/* call 2nd method */
/* give array another size ie */
std.size2 (60);
}
};
Result :
30
60
So as we can see we have changed the size of the array.
So its mean , whenever an array changes during compilation time that will be a dynamic array.
So this is quite enough for static and dynamic arrays...i hope you like it..If any problems , right now leave them down in the comments below.And i will try hard to help you out
No comments:
Post a Comment