在java中检查数组数组中元素的相邻元素

所以,我有一个看起来像的字符表


[[.,*,.,.,*]

[*,.,.,.,.]

[.,.,.,*,*]]

我想把每一个“。” 成一个数字,显示相邻字段中有多少 *。基本上是一个简单的扫雷艇。有没有一种优雅的方法来检查每个元素的每个相邻字段?因为我想到的是很多嵌套的 for 循环和 if 语句,但我确定有更好的方法来做到这一点?


编辑:预期的结果应该是这样的:


[[3,*,2,.]

 [*,*,2,.]]



喵喔喔
浏览 446回答 1
1回答

慕神8447489

我能想到的最优雅的方式是这样的:public static void main(String[] args) {&nbsp; &nbsp; char[] a = {'.', '.', '*', '.', '*'};&nbsp; &nbsp; char[] b = {'.', '*', '*', '.', '*'};&nbsp; &nbsp; char[] c = {'.', '.', '*', '.', '*'};&nbsp; &nbsp; char[] d = {'.', '*', '*', '.', '*'};&nbsp; &nbsp; char[] e = {'*', '.', '*', '.', '*'};&nbsp; &nbsp; char[][] ae = {a, b, c, d, e};&nbsp; &nbsp; char[][] numberArray = new char[5][5];&nbsp; &nbsp; for (int i = 0; i < ae.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < ae[i].length;&nbsp; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberArray[i][j] = checkAdjacentField(i, j, ae);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; StringBuilder matrix = new StringBuilder();&nbsp; &nbsp; for (char[] aNumberArray : numberArray) {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder bld = new StringBuilder("{");&nbsp; &nbsp; &nbsp; &nbsp; for (char character : aNumberArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bld.append(character).append(",");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; bld.deleteCharAt(bld.length() - 1);&nbsp; &nbsp; &nbsp; &nbsp; bld.append("}");&nbsp; &nbsp; &nbsp; &nbsp; matrix.append(bld.toString()).append("\n");&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(matrix.toString());}private static char checkAdjacentField(int i, int j, char[][] ae) {&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; if (j <= ae[i].length - 2) { // to the right&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i][j + 1] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (j <= ae[i].length - 2 && i <= ae.length -2) { // move to top right&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i + 1][j + 1] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (j <= ae[i].length - 2 && i > 0) { // move to bottom right&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i - 1][j + 1] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (j > 0) { // to the left&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i][j - 1] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (j > 0 && i <= ae.length -2) { // to top left&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i + 1][j - 1] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (j > 0 && i > 0) { // to bottom left&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i - 1][j - 1] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (i <= ae.length -2) { // move to top&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i +1][j] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; if (i > 0) { // move top bottom&nbsp; &nbsp; &nbsp; &nbsp; count += ae[i - 1][j] == '*' ? 1 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.printf("field %s, %s has %s Adjacent fields with a * \n", i, j , count);&nbsp; &nbsp; String stringValue = String.valueOf(count);&nbsp; &nbsp; return stringValue.charAt(0);}如果你对这个例子有疑问,我想听听。下一次,尝试提供一个示例,说明您之前已经准备好尝试的内容。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java