我正在完成一个程序,但在最后一个方面遇到了一些麻烦。
在程序的这一部分中,我正在测试数组是否呈锯齿状(行数和列数相同)。我想使用嵌套的for
循环来执行此操作,但遇到逻辑和结构问题。
例如,以下数组是锯齿状的:
1, 2, 3
4, 5, 6
7, 8, 9, 10
以下数组不是:
1, 2, 3
4, 5, 6
7, 8, 9
有人可以提供有关如何执行此操作的指导吗?
答案 0 :(得分:1)
首先明确锯齿状阵列是什么(坚持2D阵列作为典型案例):
(请注意,有效的锯齿状和有效的方阵是相互排斥的。)
您不需要嵌套的for
循环来检查这三个条件中的任何一个:
int[][]
声明不言而喻。for
循环 - 因为您不需要遍历包含可能不同长度的数组和的数组可能是不同长度的数组,只需迭代前者并检查后者的长度。话虽如此,请考虑以下IsJagged
和IsSquare
实施和演示条件2和3:
public class IsJaggedDemo {
private static boolean IsJagged(int[][] array) {
boolean isJagged = false;
if (array != null) {
Integer lastLength = null;
for (int i = 0; i < array.length; i++) {
if (lastLength == null) {
lastLength = array[i].length;
} else if (lastLength.equals(array[i].length)) {
continue;
} else {
isJagged = true;
break;
}
}
}
return isJagged;
}
private static boolean IsSquare(int[][] array) {
boolean isSquare = false;
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (array[i].length != array.length) {
break;
} else if (i != array.length - 1) {
continue;
} else {
isSquare = true;
}
}
}
return isSquare;
}
public static void main(String[] args) {
int[][] a = null;
int[][] b =
new int[][] {
new int[] { 1 }
};
int[][] c =
new int[][] {
new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9 }
};
int[][] d =
new int[][] {
new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9, 10 }
};
int[][] e =
new int[][] {
new int[] { 1, 2, 3 }
};
int[][] f =
new int[][] {
new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9 },
new int[] { 9, 8, 7 }
};
System.out.printf(
"a is %1$sjagged and is %2$ssquare.\r\n",
IsJagged(a) ? "" : "not ",
IsSquare(a) ? "" : "not ");
System.out.printf(
"b is %1$sjagged and is %2$ssquare.\r\n",
IsJagged(b) ? "" : "not ",
IsSquare(b) ? "" : "not ");
System.out.printf(
"c is %1$sjagged and is %2$ssquare.\r\n",
IsJagged(c) ? "" : "not ",
IsSquare(c) ? "" : "not ");
System.out.printf(
"d is %1$sjagged and is %2$ssquare.\r\n",
IsJagged(d) ? "" : "not ",
IsSquare(d) ? "" : "not ");
System.out.printf(
"e is %1$sjagged and is %2$ssquare.\r\n",
IsJagged(e) ? "" : "not ",
IsSquare(e) ? "" : "not ");
System.out.printf(
"f is %1$sjagged and is %2$ssquare.\r\n",
IsJagged(f) ? "" : "not ",
IsSquare(f) ? "" : "not ");
}
}
如果您运行演示,您应该看到以下输出:
a is not jagged and is not square.
b is not jagged and is square.
c is not jagged and is square.
d is jagged and is not square.
e is not jagged and is not square.
f is not jagged and is not square.
答案 1 :(得分:1)
如果不完全需要,为什么不使用for-each循环而不是嵌套for循环?我在几分钟前就遇到过这个问题,我认为之前的答案看起来有点复杂,尽管完全正确。
我知道这是一个多少月的问题,但这是我刚才提出的解决方案:
public static boolean isJagged(int[][] arr)
{
boolean result = false;
// for each 1D array in the 2D array arr
for (int[] a : arr)
{
if (a.length != arr[0].length)
result = true;
}
return result;
}
如果结果返回false,则2D数组不会出现锯齿,否则为真。
测试如下:
public static void main(String[] args)
{
int[][] c =
new int[][] {
new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9 }
};
int[][] d =
new int[][] {
new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9, 10 }
};
if (isJagged(c))
System.out.println("C is jagged");
else
System.out.println("C is not jagged");
if (isJagged(d))
System.out.println("D is jagged");
else
System.out.println("D is not jagged");
}
打印:
C is not jagged
D is jagged
重点是,我认为在嵌套的for循环中使用for-each循环会使用起来要复杂得多。
答案 2 :(得分:0)
在矩形二维数组中,每行的元素数量是相同的,而在锯齿状二维数组中,则不然。在方形二维数组中,每行的元素数等于行数。
public static void main(String[] args) {
int[][] arr1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9, 10}};
int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(isJagged(arr1)); // true
System.out.println(isJagged(arr2)); // false
System.out.println(isSquare(arr2)); // true
}
static boolean isJagged(int[][] arr) {
int rowLength = arr[0].length;
for (int[] row : arr)
if (row.length != rowLength)
return true;
return false;
}
static boolean isSquare(int[][] arr) {
int length = arr.length;
for (int[] row : arr)
if (row.length != length)
return false;
return true;
}
另见:How do I remove a row and a column from a jagged 2d array?