Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
推到堆栈Java后Word对象发生更改_Java_Object_Constructor - Fatal编程技术网

推到堆栈Java后Word对象发生更改

推到堆栈Java后Word对象发生更改,java,object,constructor,Java,Object,Constructor,我的类变量如下: while(!paths.isEmpty()){ BoggleSearchState2 path = paths.pop(); if(dictionary.contains(path.getWord()) && !foundWords.contains(path.getWord())){ foundWords.add(path.getWord()); } int[][] grid = path.getG

我的类变量如下:

while(!paths.isEmpty()){
    BoggleSearchState2 path = paths.pop();

    if(dictionary.contains(path.getWord()) && !foundWords.contains(path.getWord())){
            foundWords.add(path.getWord());
    }

    int[][] grid = path.getGrid();
    int row = path.getRow();
    int column = path.getColumn();
    Letter[][] game = path.getGame();

    if(row < (grid.length-1) && grid[row+1][column] == 0) {
        paths.push(new BoggleSearchState2(path.getWordWord(), game[(row+1)][column], game));
    }
}
public BoggleSearchState2(Letter l, Letter[][] gameIn) {
    game = gameIn;
    word = new Word(l);
    grid = initialGrid(l);
    row = l.getPoint().y;
    column = l.getPoint().x;
    stringWord = wordToString(word);

}

public BoggleSearchState2(Word wordIn, Letter l, Letter[][] gameIn){
    game = gameIn;
    word = new Word(wordIn.getLetters());
    word.addLetter(l);
    grid = genGrid(word, game);
    row = l.getPoint().y;
    column = l.getPoint().x;
    stringWord = wordToString(word);

}
我遇到的问题是,在我按下新的BoggleSS2之后,我的path.word发生了变化,并添加了另一个字母对象。我不知道这是怎么发生的。谁能告诉我为什么

public class BoggleSearchState2 {

    private Word word;
    private Letter[][] game;
    private int[][] grid;
    private String stringWord;
    private int row, column;
公共类单词{
私有数组词;
公共词(字母l){
word=新的ArrayList();
添加(l);
}
公共单词(ArrayList listLetters,字母l){
单词=列表字母;
添加(l);
}
公共单词(ArrayList字母){
单词=字母;
}

什么是
Word
类?Word类基本上只是一个字母对象的ArrayList。看起来您只是在复制周围的引用,所以内容的更改将反映在所有地方。您需要复制内容。请在构造函数中尝试
Word=new Word(new ArrayList(wordIn.getLetters());
public class Word {

private ArrayList<Letter> word;

public Word(Letter l) {
    word = new ArrayList<Letter>();
    word.add(l);
}
public Word(ArrayList<Letter> listLetters, Letter l){
    word = listLetters;
    word.add(l);
}
public Word(ArrayList<Letter> letters) {
    word = letters;
}