在Java中创建向量并在递归方法中存储信息
编辑 有人向我指出,实际上,我需要逐步完成所有递归,以确保达到终点,然后在返回堆栈时开始收集值。我不知道这意味着什么 我的目标是构建一个递归方法来构建一条穿过迷宫的路径,将该路径存储在向量中,并在递归结束时返回它 我的方法运行迷宫,但我很难将列表存储在向量中,因为每次递归都需要实例化它 这是我的代码(编辑过的):在Java中创建向量并在递归方法中存储信息,java,recursion,vector,Java,Recursion,Vector,编辑 有人向我指出,实际上,我需要逐步完成所有递归,以确保达到终点,然后在返回堆栈时开始收集值。我不知道这意味着什么 我的目标是构建一个递归方法来构建一条穿过迷宫的路径,将该路径存储在向量中,并在递归结束时返回它 我的方法运行迷宫,但我很难将列表存储在向量中,因为每次递归都需要实例化它 这是我的代码(编辑过的): protectedvectorfindpath(int nRow,int nCol) { 在[nRow][nCol]上。已访问(真实); 如果(在[nRow][nCol].getVal
protectedvectorfindpath(int nRow,int nCol)
{
在[nRow][nCol]上。已访问(真实);
如果(在[nRow][nCol].getVal()上='E')
{
向量列表=新向量();
添加(船上[nRow][nCol]);
退货清单;
}
if(canGoLeft(nRow,nCol))
{
如果(!on[nRow][nCol-1].isVisited())
{
返回findPath(nRow,--nCol);
}
}
if(仓位(nRow,nCol))
{
如果(!船上[nRow][nCol+1].isVisited())
{
返回findPath(nRow,++nCol);
}
}
if(canGoUp(nRow,nCol))
{
如果(!登上[nRow-1][nCol].isVisited())
{
返回findPath(-nRow,nCol);
}
}
if(canGoDown(nRow,nCol))
{
如果(!在[nRow+1][nCol].isVisited()上)
{
返回findPath(++nRow,nCol);
}
}
System.out.println(“你遇到了死胡同。”);
返回null;
}
有没有一种方法我不能每次递归都实例化我的向量,这样我就可以永久地存储我的值
任何帮助都将不胜感激。完成此操作的典型方式是委托给另一个方法,即递归方法,并将要填充的列表作为参数:
public List<GameCell> findPath(int nRow, int nCol) {
List<GameCell> result = new ArrayList<>();
doFindPathRecursively(result, nRow, nCol);
return result;
}
private void doFindPathRecursively(List<GameCell> result, int nRow, int nCol) {
...
}
公共列表findPath(int nRow,int nCol){
列表结果=新建ArrayList();
DoFindPathRecursive(结果、nRow、nCol);
返回结果;
}
私有void dofindpath递归(列表结果、int nRow、int nCol){
...
}
请注意,自Java 2以来,Vector实际上已被弃用,因此改用List和ArrayList。我找到了解决方案,在我找到返回递归堆栈的路径后,我不得不向列表中添加元素。这是解决办法。我仍然有一个错误,但我问的问题是固定的
protected Vector<GameCell> findPath(int nRow, int nCol)
{
this.aBoard[nRow][nCol].setVisited(true);
if(this.aBoard[nRow][nCol].getVal() == 'E')
{
Vector<GameCell> list = new Vector<GameCell>();
list.add(this.aBoard[nRow][nCol]);
return list;
}
if(canGoUp(nRow, nCol))
{
if(!this.aBoard[nRow - 1][nCol].isVisited())
{
Vector<GameCell> list = findPath(nRow - 1, nCol);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
if(canGoRight(nRow, nCol))
{
if(!this.aBoard[nRow][nCol+1].isVisited())
{
Vector<GameCell> list = findPath(nRow, nCol + 1);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
if(canGoLeft(nRow, nCol))
{
if(!this.aBoard[nRow][nCol - 1].isVisited())
{
Vector<GameCell> list = findPath(nRow, nCol - 1);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
if(canGoDown(nRow, nCol))
{
if(!this.aBoard[nRow + 1][nCol].isVisited())
{
Vector<GameCell> list = findPath(nRow + 1, nCol);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
return null;
}
protectedvectorfindpath(int nRow,int nCol)
{
这个。在[nRow][nCol]上。setVisited(真);
如果(this.on[nRow][nCol].getVal()='E')
{
向量列表=新向量();
列表。添加(本文件[nRow][nCol]);
退货清单;
}
if(canGoUp(nRow,nCol))
{
如果(!this.on[nRow-1][nCol].isVisited())
{
向量列表=findPath(nRow-1,nCol);
如果(列表!=null)
{
列表。添加(本文件[nRow][nCol]);
退货清单;
}
}
}
if(仓位(nRow,nCol))
{
如果(!this.on[nRow][nCol+1].isVisited())
{
向量列表=findPath(nRow,nCol+1);
如果(列表!=null)
{
列表。添加(本文件[nRow][nCol]);
退货清单;
}
}
}
if(canGoLeft(nRow,nCol))
{
如果(!this.on[nRow][nCol-1].isVisited())
{
向量列表=findPath(nRow,nCol-1);
如果(列表!=null)
{
列表。添加(本文件[nRow][nCol]);
退货清单;
}
}
}
if(canGoDown(nRow,nCol))
{
如果(!this.on[nRow+1][nCol].isVisited())
{
向量列表=findPath(nRow+1,nCol);
如果(列表!=null)
{
列表。添加(本文件[nRow][nCol]);
退货清单;
}
}
}
返回null;
}
是。将它作为参数传递给方法,并从方法中创建它。我应该澄清,我设置的参数是根据我的规范可以使用的唯一参数。我的向量必须用这个方法创建。java.util.Vector
已经过时19年了。你用Java编程多久了?没多久,这是我们程序中需要的规范。我们的指导老师编程的时间比这长得多。不幸的是,我不能使用外部方法来实现这一点;否则,这将是一个容易得多的任务。我更新了我的问题,使我的要求更加明确。这看起来像是一个武断的、不切实际的要求。但您也可以在每次调用时返回创建和返回列表,并将返回的列表附加到当前列表,以实现相同的效果。似乎一致的是,仍然使用Vector
的教师会分发带有任意和不现实要求的作业……修复了最后一个错误,现在一切正常。在嵌套的if语句中,我有预递增和递减,我应该用+1或-1代替。不是--nRow,而是nRow-1。在我的列表中的2个值(而不是1)返回null和null之后,递归返回到它们。
protected Vector<GameCell> findPath(int nRow, int nCol)
{
this.aBoard[nRow][nCol].setVisited(true);
if(this.aBoard[nRow][nCol].getVal() == 'E')
{
Vector<GameCell> list = new Vector<GameCell>();
list.add(this.aBoard[nRow][nCol]);
return list;
}
if(canGoUp(nRow, nCol))
{
if(!this.aBoard[nRow - 1][nCol].isVisited())
{
Vector<GameCell> list = findPath(nRow - 1, nCol);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
if(canGoRight(nRow, nCol))
{
if(!this.aBoard[nRow][nCol+1].isVisited())
{
Vector<GameCell> list = findPath(nRow, nCol + 1);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
if(canGoLeft(nRow, nCol))
{
if(!this.aBoard[nRow][nCol - 1].isVisited())
{
Vector<GameCell> list = findPath(nRow, nCol - 1);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
if(canGoDown(nRow, nCol))
{
if(!this.aBoard[nRow + 1][nCol].isVisited())
{
Vector<GameCell> list = findPath(nRow + 1, nCol);
if(list != null)
{
list.add(this.aBoard[nRow][nCol]);
return list;
}
}
}
return null;
}