0

How do I modify "list" passed into validCell, in validCell, then return the modified list? validCell takes the parameters and checks to see if a path of cells to spell out "word" can be found from the starting point given by r & c of the for loop in cellsForWord. I don't think what I have is correct.

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;
}

}

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
EZ_
  • 53
  • 1
  • 6

3 Answers3

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.

Community
  • 1
  • 1
Michael
  • 1,014
  • 1
  • 8
  • 23
  • Everything in java is pass by value. Read second answer to understand how it works. http://stackoverflow.com/questions/40480/is-java-pass-by-reference – kosa Aug 09 '12 at 05:12
  • I believe what I said is still correct. The effect is passing by value, as in from the programmer's perspective, it's byval. However, at its core, it's byref. Hence the answer by @erlando. Corrected my post none-the-less. – Michael Aug 09 '12 at 05:14
  • 1
    Yes, I didn't complain your answer is wrong, but only one word was wrong. Now you corrected it. Good answer. – kosa Aug 09 '12 at 05:17
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.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

Consider converting the list into a class member. Then you don't have to pass it to the methods:

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)
   }
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268