Arrays
Arrays
An array represents a group of elements of same data type.We can represents several elements in a single array
Three types of array
One dimensional array
Two dimensional array
Syntax:-
dataType[] arr;
(or)
dataType []arr;
(or)
dataType arr[];
Instantiation of an Array in Java:-
arrayname=new datatype[size];Example:-
class Test{
public static void main(String args[]){
int a[]=new int[5]; //declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}}
Output:-10 20 70 40 50
Declaration, Instantiation and Initialization of Java Array:-
int a[]={33,3,4,5}; //declaration, instantiation and initialization
{
public static void main(String args[]){
int a[]={33,3,4,5}; //declaration, instantiation and initialization
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}}
Output:-
33
3
4
5
Syntax:-
dataType[][] arrayRefVar;
(or)
dataType [][]arrayRefVar;
(or)
dataType arrayRefVar[][];
(or)
dataType []arrayRefVar[];
Example:-
public static void main(String args[]){
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(a4 4 5rr[i][j]+" ");
}
System.out.println();
}
}}
Output:-
1 2 3
2 4 5
Comments
Post a Comment