2D array in Java
- It is a collection of data of same data type.
- It is used to store group of data simultaneously.
- It can store data of same data type means an integer array can store only integer value ,character array can store only character value and so on.
- We can not fetch data from array directly therefore we use index point.
- The indexing of array always start with 0.
- Index value is always an integer number.
- Array may be of any data type like int,char,float etc.
- Here a is the name of array.
- int is the data type of array.
- Size of array is 3x3 means we can store maximum 9 values in this array.
int a[3][3]={ {40,50,60},{10,20,30},{70,80,90} };
int a[][]=new int[3][3]; a[0][0]=40; a[0][1]=50; a[0][2]=60; a[1][0]=10; a[1][1]=20; a[1][2]=30; a[2][0]=70; a[2][1]=80; a[2][2]=90;
import java.util.Scanner; class Easy { public static void main(String[] args) { int a[][]={{40,50,60},{10,20,30},{70,80,90}};//Initializing array System.out.println("value at a[0][0]="+a[0][0]); System.out.println("value at a[0][1]="+a[0][1]); System.out.println("value at a[0][2]="+a[0][2]); System.out.println("value at a[1][0]="+a[1][0]); System.out.println("value at a[1][1]="+a[1][1]); System.out.println("value at a[1][2]="+a[1][2]); System.out.println("value at a[2][0]="+a[2][0]); System.out.println("value at a[2][1]="+a[2][1]); System.out.println("value at a[2][2]="+a[2][2]); } } /* ### Output ### value at a[0][0]=40 value at a[0][1]=50 value at a[0][2]=60 value at a[1][0]=10 value at a[1][1]=20 value at a[1][2]=30 value at a[2][0]=70 value at a[2][1]=80 value at a[2][2]=90 */
class Easy { public static void main(String[] args) { int a[][]={{40,50,60},{10,20,30},{70,80,90}};//Initializing array for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { System.out.print(a[i][j]+" "); } System.out.println(""); } } } /* ### Output ### 40 50 60 10 20 30 70 80 90 */
import java.util.Scanner; class Easy { public static void main(String[] args) { Scanner in=new Scanner(System.in); int a[][]=new int[3][3]; int i,j; System.out.println("Enter 9 value one by one"); //taking input for(i=0;i<=2;i++) for(j=0;j<=2;j++) a[i][j]=in.nextInt(); //printing element of array System.out.println("Element is given below"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { System.out.print(a[i][j]+" "); } System.out.println(""); } } } /* ### Output ### Enter 9 value one by one 4 8 2 3 7 9 2 8 1 Element is given below 4 8 2 3 7 9 2 8 1 */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other. Thanks.😊
0 Comments