Skip to content

Instantly share code, notes, and snippets.

@BT-ICD
Created April 1, 2021 14:47
Show Gist options
  • Save BT-ICD/7c2fa5d24d59c30f8886d22589260380 to your computer and use it in GitHub Desktop.
Save BT-ICD/7c2fa5d24d59c30f8886d22589260380 to your computer and use it in GitHub Desktop.
Example of Two Dimension Array
/**
* Example: 1 - Two Dimension Array
* length of array - initialize array and iterate each element of an array
* */
public class TwoDimArrayDemo1 {
public static void main(String[] args) {
//Declaration of two dimension array. Having two rows and three columns
int[][] data = new int[2][3];
//Initialize values of first row
data[0][0]= 10;
data[0][1]= 20;
data[0][2]= 30;
//Initialize values of second row
data[1][0]= 60;
data[1][1]= 70;
data[1][2]= 80;
//length to get total number rows of array
System.out.println("Number of rows: " + data.length);
//length - number of columns in 0th row
System.out.println("Number of columns in 0th row: " + data[0].length);
System.out.println("Number of columns in 1st row: " + data[1].length);
//To print all the elements like matrix
for(int i=0;i<data.length;i++){
System.out.println("");
for(int j =0;j<data[i].length;j++){
System.out.print(data[i][j]+ "\t");
}
}
}
}
@BT-ICD
Copy link
Author

BT-ICD commented Apr 1, 2021

Sample output

Number of rows: 2
Number of columns in 0th row: 3
Number of columns in 1st row: 3

10 20 30
60 70 80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment