I am a programming novice, learning programming knowledge from 0.
Let's start with a topic: write a piece of code that prints the contents of a two-dimensional boolean array. Where * means true and space means false. Print row and column numbers.
Code:
public class Ex_11 { public static void printBooleans(boolean[][] a){ int rowNum = a.length; int colNum=0; if(rowNum>0) colNum = a[0].length; for(int i=0;i<rowNum;i++){ for(int j=0;j<colNum;j++){ if(a[i][j])System.out.print("["+i+"]["+j+"]"+"*"); else System.out.print("["+i+"]["+j+"]"+" "); } System.out.println(); } } public static void main(String[] args) { // TODO Auto-generated method stub boolean[][] b = {{true,true,false,false},{false,false,true,true}}; boolean[][] c={}; printBooleans(b); printBooleans(c); } }
The topic is very simple, new knowledge learned:
1. A two-dimensional array is essentially a one-dimensional array, the number of rows = array name.length, the number of columns = array name[0].length;
2. Number of columns = array name [0].length Before this sentence, it is necessary to judge whether the array is empty. If it is empty, the sub-number represented by the array name [0] does not exist, and an error will be reported.
public class Ex_11 {
public static void printBooleans(boolean[][] a){int rowNum = a.length;int colNum=0;if(rowNum>0)colNum = a[0].length;for(int i=0;i<rowNum;i++){for(int j=0;j<colNum;j++){if(a[i][j])System.out.print("["+i+"]["+j+"]"+"*");else System.out.print("["+i+"]["+j+"]"+" ");}System.out.println();}}
public static void main(String[] args) {// TODO Auto-generated method stubboolean[][] b = {{true,true,false,false},{false,false,true,true}};boolean[][] c={};printBooleans(b);printBooleans(c);}
}