0

如何在validCell中修改传递给validCell的“列表”,然后返回修改后的列表?validCell 获取参数并检查是否可以从 cellsForWord 中 for 循环的 r & c 给出的起点找到拼写“单词”的单元格路径。我不认为我所拥有的是正确的。

public class GoodWordOnBoardFinder implements IWordOnBoardFinder {

@Override
public List<BoardCell> cellsForWord(BoggleBoard board, String word) {
    // TODO Auto-generated method stub
    List<BoardCell> list = new ArrayList<BoardCell>();
    //Loop through each cell on board to find a starting point
       for(int r=0; r < board.size(); r++)
       {
           for(int c=0; c < board.size(); c++)
           {
              if(validCell(board, r, c, list, word, 0))
                  return list;
                   //***HOW to get populated list NOT Blank list???
           }
       }
    return null;
}
public boolean validCell(BoggleBoard theBoard, int row, int col, List<BoardCell> cList, String theWord, int letterIndex ){

    BoardCell cell = new BoardCell(row, col);

   String letter = theWord.substring(letterIndex, letterIndex+1);
    //Check the whole world has been found
   if(letterIndex >= theWord.length())
       return true;

   //Check if row or column is off the board
   if(row > theBoard.size() || col > theBoard.size())
       return false;
   //Check if cell has already been visited
   if(cList.contains(cell))
       return false;
   //Check if cell face isn't the letter we're looking for
   if(!theBoard.getFace(row, col).equals(letter))
   {
       //Make sure the letter isn't a Q bc Boggle is special
       if(!(theBoard.getFace(row, col).equals("Qu") && letter.equals("q")))
           return true;
   }
   cList.add(cell); 

  //Check all neighboring cells for letters of the word
  int[] rdelta = {-1,-1,-1, 0, 0, 1, 1, 1};
  int[] cdelta = {-1, 0, 1,-1, 1,-1, 0, 1};
  for(int k=0; k < rdelta.length; k++){
    if (validCell(theBoard, row+rdelta[k], col+cdelta[k], cList, theWord, letterIndex+1))
        return true;
   }
  cList.remove(cell);
return false;
}

}

4

3 回答 3

0

In Java, Objects are passed byval (see this response). This essentially means that a reference to the object itself is passed in, and translated back into the original object, not a copy of the object. You can just modify list and not worry about returning it, as any modifications will be kept.

Example:
List<String> test = new ArrayList<String>();
test.add("Test 1");
OtherClass otherClass = new OtherClass(test);
...
otherClass.modifyList(test);
...
System.out.println(test.size());

In this example, modifyList will hypothetically add another string to the passed in list.

于 2012-08-09T05:10:00.980 回答
0

Any changes you make cList inside the validCell will automatically be reflected to the original list.

Java is pass by value: In this case it is passing the value of the reference. So both will be pointing to the same object. Any changes made in validCell to cList will reflect on list.

于 2012-08-09T05:10:53.910 回答
0

考虑将列表转换为类成员。然后你不必将它传递给方法:

public class GoodWordOnBoardFinder implements IWordOnBoardFinder {

   // The board
   List<BoardCell> board;

   // methods
   public boolean validCell(BoggleBoard theBoard, int row, int col, String theWord, int letterIndex ){
     // access/modify the board (= what you called cList)
   }
}
于 2012-08-09T05:25:30.467 回答