java二维数组如何求行数_JAVA中如何获取一个二维数组的行数和列数

我是个编程菜鸟,在从0开始学习编程知识。

由一道题目说开去:编写一段代码,打印出一个二维布尔数组的内容。其中*表示真,空格表示假。打印出行号和列号。

代码:

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

for(int j=0;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);

}

}

题目很简单,学到的新知识:

1.二维数组本质是一维数组,行数=数组名.length,列数=数组名[0].length;

2.列数=数组名[0].length这句之前要判断数组是否为空,若为空则数组名[0]所代表的子数字不存在,会报错。

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

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);}

}

来源:https://www.cnblogs.com/wudi-tk/p/7594674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值