Created
April 1, 2021 14:52
-
-
Save BT-ICD/f46d6e40294842a80c3bf903c4a9400e to your computer and use it in GitHub Desktop.
Initialize elements of Two Dimension Array during declaration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example: 2 - Two Dimension Array | |
* To initialize elements of an array while declaration | |
* */ | |
public class TwoDimArrayDemo2 { | |
public static void main(String[] args) { | |
int[][] data = {{2,3,4},{10,20,30}, {7,8,9}}; | |
//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"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Output
Number of rows: 3
Number of columns in 0th row: 3
Number of columns in 1st row: 3
2 3 4
10 20 30
7 8 9