1 回答
TA贡献1795条经验 获得超7个赞
你的逻辑看起来几乎是正确的。我看到的唯一问题是处理两个数组都不为空且具有相同第一维的情况的逻辑。如果任何索引没有匹配的长度,您应该返回 false:
public static boolean arraySameSize(int[][] a, int[][] b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length != b.length) {
return false;
}
// if the code reaches this point, it means that both arrays are not
// null AND both have the same length in the first dimension
for (int i=0; i < a.length; i++) {
if (a[i] == null && b[i] == null) {
continue;
}
if (a[i] == null || b[i] == null) {
return false;
}
if (a[i].length != b[i].length) {
return false;
}
}
return true;
}
按照下面的演示链接查看此方法正常工作的一些示例。
添加回答
举报