Java 如何在方法之间修改列表?
如何在validCell中修改传递到validCell的“列表”,然后返回修改后的列表?validCell获取参数并检查是否可以从CellsForward中for循环的r&c给定的起点找到拼写“word”的单元格路径。我认为我所说的不正确Java 如何在方法之间修改列表?,java,Java,如何在validCell中修改传递到validCell的“列表”,然后返回修改后的列表?validCell获取参数并检查是否可以从CellsForward中for循环的r&c给定的起点找到拼写“word”的单元格路径。我认为我所说的不正确 public class GoodWordOnBoardFinder implements IWordOnBoardFinder { @Override public List<BoardCell> cellsForWord(BoggleBoar
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;
}
公共类GoodWordOnBoardFinder实现IWordOnBoardFinder{
@凌驾
公共列表单元格WORWORD(BoggleBoard板,字符串字){
//TODO自动生成的方法存根
列表=新的ArrayList();
//在板上的每个单元格中循环以找到起点
对于(int r=0;r=theWord.length())
返回true;
//检查行或列是否脱离板
如果(行>theBoard.size()| |列>theBoard.size())
返回false;
//检查是否已访问过该单元
if(cList.contains(单元格))
返回false;
//检查一下我们要找的信是否是cell face
如果(!theBoard.getFace(行,列).equals(字母))
{
//确保这封信不是Q bc Boggle是特殊的
if(!(board.getFace(row,col).equals(“Qu”)&letter.equals(“q”))
返回true;
}
cList.add(单元格);
//检查所有相邻单元格中的单词字母
int[]rdelta={-1,-1,0,0,1,1,1,1};
int[]cdelta={-1,0,1,-1,1,-1,0,1};
for(int k=0;k
}在Java中,对象是通过VAL传递的(请参阅)。这本质上意味着传入对对象本身的引用,并将其转换回原始对象,而不是对象的副本。您只需修改列表,不必担心返回列表,因为任何修改都会保留下来
Example:
List<String> test = new ArrayList<String>();
test.add("Test 1");
OtherClass otherClass = new OtherClass(test);
...
otherClass.modifyList(test);
...
System.out.println(test.size());
示例:
列表测试=新建ArrayList();
测试。添加(“测试1”);
OtherClass OtherClass=新的OtherClass(测试);
...
其他类。修改列表(测试);
...
System.out.println(test.size());
在本例中,
modifyList
将假设向传入列表中添加另一个字符串。您在validCell中所做的任何更改都将自动反映到原始列表中
Java是按值传递的:在本例中,它传递的是引用的值。所以两者都指向同一个对象。validCell中对
cList
所做的任何更改都将反映在list
上,请考虑将列表转换为类成员。然后,您不必将其传递给方法:
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)
}
}
公共类GoodWordOnBoardFinder实现IWordOnBoardFinder{
//董事会
名单板;
//方法
公共布尔有效单元格(BoggleBoard-theBoard、int-row、int-col、String-theWord、int-letterIndex){
//访问/修改电路板(=您称之为cList)
}
}
java中的一切都是按值传递的。阅读第二个答案,了解其工作原理。我相信我所说的仍然是正确的。效果是通过值传递的,从程序员的角度来看,它是byval。然而,它的核心是byref。因此,@erlando给出了答案。是的,我没有抱怨你的答案是错的,但只有一个字是错的。现在你改正了。回答得好。