首先,这里是相关代码:
public class AtCounter
{
private char[][] atMat;
private int atCount;
public AtCounter()
{
atCount=0;
atMat= new char[][]{{'@','-','@','-','-','@','-','@','@','@'},
{'@','@','@','-','@','@','-','@','-','@'},
{'-','-','-','-','-','-','-','@','@','@'},
{'-','@','@','@','@','@','-','@','-','@'},
{'-','@','-','@','-','@','-','@','-','@'},
{'@','@','@','@','@','@','-','@','@','@'},
{'-','@','-','@','-','@','-','-','-','@'},
{'-','@','@','@','-','@','-','-','-','-'},
{'-','@','-','@','-','@','-','@','@','@'},
{'-','@','@','@','@','@','-','@','@','@'}};
}
public int countAts(int r, int c)
{
if(r<atMat.length && c<atMat[0].length && r>=0 && c>=0 && atMat[r][c]=='@')
{
atMat[r][c]='X';
atCount++;
}
else
if(r-1<atMat.length && r-1>=0)
countAts(r-1,c);
else
if(r+1<atMat.length && r+1>=0)
countAts(r+1,c);
else
if(c-1<atMat[0].length && c-1>=0)
countAts(r,c-1);
else
if(c+1<atMat[0].length && c+1>=0)
countAts(r,c+1);
return atCount;
}
请注意,
countsAts
方法包含atMat[r][c]='X';
行,该行更改了二维数组中元素的值。尽管如此,在private char[][] atMat;
行上,Netbeans给我警告“ Field atMat可能是最终的”。如果我修改其值,为什么该字段可以是最终值?它与我当前正在修改的代码中的位置有关吗? Netbeans是否被窃听(怀疑)?如果有人可以解释警告,我将不胜感激! 最佳答案
您可以更改名称为atMat的对象的内容。 atMat是参考。引用本身可以成为最终的。