gpt4 book ai didi

java - 为什么方法会更改 boolean 数组参数?

转载 作者:行者123 更新时间:2023-12-01 23:08:26 25 4
gpt4 key购买 nike

我有一个类(class),用于寻找穿过基本迷宫的路径。迷宫连同起始位置和结束位置一起传递到路径。我使用递归来查找点之间的路径。该路径以 boolean 数组的形式返回。由于某种原因,传递给 Path() 的初始迷宫发生了变化。因此,我尝试制作迷宫数组的副本以避免更改任何值,但它仍然不起作用。

为什么 path() 对 open[][] 进行更改?可能是 C 编程让我困惑。

public static boolean[][] path(boolean[][] open, 
int start_i, int start_j,
int end_i, int end_j)
{
int n = open.length;
boolean[][] openCopy = new boolean[n][n]; //make a copy of open

for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
openCopy[i][j] = open[i][j];
}
}
return findPath(openCopy, start_i, start_j, end_i, end_j);
}

public static boolean[][] findPath(boolean[][] openCopy, int start_i, int start_j, int end_i, int end_j)
{
boolean[][] path = new boolean[openCopy.length][openCopy[0].length];

if(openCopy[start_i][start_j] == false) //return false if current position is not open
return path;
else
openCopy[start_i][start_j] = false; //make current position false if not (to prevent infinite backtracking)

if(start_i == end_i && start_j == end_j) //if end found return true
{
path[start_i][start_j] = true;
return path;
}

path = findPath(openCopy, start_i+1, start_j, end_i, end_j); // Move North
path = findPath(openCopy, start_i, start_j+1, end_i, end_j); // Move East
path = findPath(openCopy, start_i-1, start_j, end_i, end_j); // Move South
path = findPath(openCopy, start_i, start_j-1, end_i, end_j); // Move West

return path;
}

最佳答案

事实并非如此,我知道这可能不是该方法完全“合适”的输入,但它证明了这一点:

public class MainForm {


public static void main(String[] args) {
boolean[][] array = new boolean[][] {{false, false, true, false, false, false},
{false, false, true, false, false, false},
{false, false, true, false, false, false},
{false, false, true, false, false, false}};

boolean[] [] another = path(array, 0, 0, 3, 5);

for (boolean[] bArray : array) {
for (boolean b : bArray) {
System.out.println(b);
}
}
System.out.println("***********************");
for (boolean[] bArray : another) {
for (boolean b : bArray) {
System.out.println(b);
}
}
}

public static boolean[][] path(boolean[][] open, int start_i, int start_j, int end_i, int end_j) {
int n = open.length;
boolean[][] openCopy = new boolean[n][n]; // make a copy of open

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
openCopy[i][j] = open[i][j];
}
}
return findPath(openCopy, start_i, start_j, end_i, end_j);
}

public static boolean[][] findPath(boolean[][] openCopy, int start_i, int start_j, int end_i, int end_j) {
boolean[][] path = new boolean[openCopy.length][openCopy[0].length];

if (openCopy[start_i][start_j] == false) // return false if current position is not open
return path;
else
openCopy[start_i][start_j] = false; // make current position false if not (to prevent infinite backtracking)

if (start_i == end_i && start_j == end_j) // if end found return true
{
path[start_i][start_j] = true;
return path;
}

path = findPath(openCopy, start_i + 1, start_j, end_i, end_j); // Move North
path = findPath(openCopy, start_i, start_j + 1, end_i, end_j); // Move East
path = findPath(openCopy, start_i - 1, start_j, end_i, end_j); // Move South
path = findPath(openCopy, start_i, start_j - 1, end_i, end_j); // Move West

return path;
}
}

输出

false
false
true
false
false
false
false
false
true
false
false
false
false
false
true
false
false
false
false
false
true
false
false
false
***********************
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false

显然,返回的数组和传递给path()的数组内容不同,而原始数组的原始值保持不变。我认为问题出在您的应用程序的其他地方。

关于java - 为什么方法会更改 boolean 数组参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22414624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com