Java Array consist of two types as follows.
- Single Dimensional Array
- Multi Dimensional Array
Following is the syntax to declare single dimensional array.
[java]dataType[] arr;[/java]
Following is an example which describes more about the single dimensional array.
singledimentional.java
[java]public class singledimentional {
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=100;//initialization
a[1]=200;
a[2]=870;
a[3]=640;
a[4]=850;
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}[/java]
Output
Now compile the code, result will be as follows.
[java]
100
200
870
640
850[/java]
Following is the syntax to declare multi dimensional array.
[java]dataType[][] arrayRefVar;[/java]
Following is an example which describes more about the multi dimensional array.
multidimensional.java
[java]public class multidimensional {
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}[/java]
Output
When compile the code, resutl will be as follows.
[java]
1 2 3
2 4 5
4 4 5 [/java]