如何修复堆栈? (更具体的描述)

时间:2016-04-19 03:48:34

标签: java static stack

import java.io.*;
import java.util.*;

public class CookieMonster1 {

public static int [][] maze;
public static int counter;
public static Stack<CookiePosition> path;           
public static CookiePosition posLast2;

public static void main(String[] args){

    path = new Stack<CookiePosition>();
    CookiePosition posCurrent = new CookiePosition();
    posLast2 = new CookiePosition();
    path.push(posCurrent);
    maze = getMaze();
    counter = maze[0][0];
    System.out.print(path);

    while(posCurrent.getX()!=11 && posCurrent.getY()!=11)
    {
        posCurrent = move(posCurrent);                                  // make a move
                                                // output the maze-just to check if working         
        for(int row=0; row<maze.length; row++)
        {
            for(int col=0; col<maze[row].length; col++)
            {
                if(posCurrent.getX()==row && posCurrent.getY()==col)
                    System.out.print("[!]");
                else if(maze[row][col]==-1)
                    System.out.print("[*]");
                else
                    System.out.print("["+maze[row][col]+"]");   
            } 
            System.out.println();
        }  
    }

    System.out.println(counter);
} 

//checks if the pointer is able to move to the next position
public static boolean canMove(int x, int y)
{
    if(x!=maze.length && y!=maze[x].length && maze[x][y]!=-1)
        return true;
    return false;   
}

//makes cookie position final
public static CookiePosition lock(CookiePosition x){
    final CookiePosition hold = x;
    return hold;
}

public static CookiePosition move(CookiePosition posCurrent1)
{   
    System.out.println("Before Move "+path.peek());
                                        // check can move right or down, move down
    if(canMove((int)posCurrent1.getX()+1, (int)posCurrent1.getY()) && canMove((int)posCurrent1.getX(), (int)posCurrent1.getY()+1))
    {
        System.out.println("Both");
        posLast2.setX(posCurrent1.getX());                      // remember last spot with both moves
        posLast2.setY(posCurrent1.getY());
                                        //if can move either way or down always down
        posCurrent1.setX(posCurrent1.getX()+1);
        path.push(lock(posCurrent1));
        counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()];
        System.out.println(counter + " Cookies");
        //maze[pos1.getX()][pos1.getY()]=0;
        System.out.println(path);

    }
                                            // check if can move down
    else if (canMove((int)posCurrent1.getX()+1, (int)posCurrent1.getY()))
    {
        posCurrent1.setX(posCurrent1.getX()+1);
        path.push(lock(posCurrent1));           
        counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()];
        System.out.println(counter + " Cookies");
        //maze[pos1.getX()][pos1.getY()]=0;
        System.out.println(path);
    }           
                                            // check can move right
    else if(canMove((int)posCurrent1.getX(), (int)posCurrent1.getY()+1))
    {
        System.out.println("Right");
        posCurrent1.setY(posCurrent1.getY()+1); 
        path.push(lock(posCurrent1));
        counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()];
        System.out.println(counter + " Cookies");
        //maze[pos1.getX()][pos1.getY()]=0;
        System.out.println(path);
    }
                                            // cannot move, back up
    else
    {
        System.out.println("Go back to last spot where 2 moves were possible.");  // test line
        while(posCurrent1.getX()!=posLast2.getX() || posCurrent1.getY()!=posLast2.getY())
        {
            System.out.println(path);
            System.out.println("Removed "+ path.pop());
            counter-=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()];
            System.out.println("Stack empty? "+path.empty());  // test line
            System.out.println("Current Top "+path.peek());    // test line
            posCurrent1=path.peek();
            System.out.println(counter + "Cookies");
        }
        posCurrent1.setY(posCurrent1.getY()+1);
        path.push(new CookiePosition((int)posCurrent1.getY(), (int)posCurrent1.getY()));
        counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()];
        //maze[pos1.getX()][pos1.getY()]=0;
    }
    System.out.println("After Move "+path.peek());   // test line
    return posCurrent1;
}

//reads the maze from the text file 
public static int[][] getMaze()
{
    File file = new File("cookies.txt");
    Scanner input = null;
    try
    {
        input = new Scanner(file);
    }
    catch (FileNotFoundException ex)
    {
        System.out.println("Cannot open file");
        System.exit(1);
    }
    int [][] maze = new int[12][12];
    int row, col, temp;

    for (row = 0; row<maze.length; row++)
        for (col = 0; col<maze[row].length;col++)
            maze[row][col] = input.nextInt();
    for (row = 0; row<maze.length; row++)
    {
        for (col = 0; col<maze[row].length;col++)
            System.out.print(maze[row][col]+"  ");
        System.out.println();

    }
    return maze;
}

}

当我编译并运行此代码时,一切正常,直到最终&#34;否则&#34;阻止移动方法。我已将问题缩小到堆栈。当我打印它时,它会打印所有相同的坐标。但是,所有坐标应该不同。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

<强>问题

问题在于方法

的返回值
public static CookiePosition move(CookiePosition posCurrent1)

上述方法接受当前位置,修改值并返回它。 以下方法每次都返回相同的引用,但内部字段已更改。

posCurrent = move(posCurrent); 

因此实际上堆栈以一组对象结束,所有对象都指向同一个对象引用。这解释了为什么堆栈为其所有元素显示相同的坐标值。

<强>解决方案

确保方法move(posCurrent)返回CookiePosition的新实例。您可以为CookiePosition创建一个新的构造函数,它将从现有实例中读取值。

E.g. return new CookiePosition(posCurrent);