首先,这是相关代码:
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给了我警告&#34; Field atMat可以是最终的#34;。如果我要修改它的值,为什么这个字段能够成为最终字段?它是否与我正在修改它的代码中的 where 有关? Netbeans是否被窃听(怀疑)?如果有人能解释警告,我将非常感激!
答案 0 :(得分:4)
您可以更改名称atMat引用的对象的内容。 atMat是一个参考。引用本身可以是最终的。