Array in java
Generally In array is collection of similar type of elements which have a contiguous memory
located.
Array in java is index based , the first element of the array is stored at 0th index
2nd element is stored on 1st index and so on .
Advantage
* Code Optimization : it make the code optimized, we can retrieve or sort the data
efficiently.
* Random access: We can get any data using the index position.
Disadvantages of array
* 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.
Type Of Array
* Single Dimensional Array
* Multidimensional Array
1. Declaration an array.
dataType[] arr; //and
dataType[]arr;//and
dataType arr[];
Instantiation of array
arrayvar=new dataType[size];
For Example :
class TestArray
{
public static void main(String[]args)
{
int [x]=new int[5]; // there is declaration and instantition
x[0]=10;
x[1]=20;
x[2]=30;
x[3]=40;
x[4]=50;
for(int i=0;i<x.length;i++)
{
System.out.prinltn(x[i]);
}
}
}
Output :
10
20
30
40
50
Generally In array is collection of similar type of elements which have a contiguous memory
located.
Array in java is index based , the first element of the array is stored at 0th index
2nd element is stored on 1st index and so on .
Advantage
* Code Optimization : it make the code optimized, we can retrieve or sort the data
efficiently.
* Random access: We can get any data using the index position.
Disadvantages of array
* 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.
Type Of Array
* Single Dimensional Array
* Multidimensional Array
1. Declaration an array.
dataType[] arr; //and
dataType[]arr;//and
dataType arr[];
Instantiation of array
arrayvar=new dataType[size];
For Example :
class TestArray
{
public static void main(String[]args)
{
int [x]=new int[5]; // there is declaration and instantition
x[0]=10;
x[1]=20;
x[2]=30;
x[3]=40;
x[4]=50;
for(int i=0;i<x.length;i++)
{
System.out.prinltn(x[i]);
}
}
}
Output :
10
20
30
40
50
0 comments:
Post a Comment