声明的变量
public final static int[][] highscores = new int[3][5];
public final static String[][] players = new String[][]{
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
};
对其进行如下操作
public static void addScore(int diff, String player, int score){
for(int i = 0; i < 5; i++){
if(score > highscores[diff - 3][i]){
for(int j = 4; j > i; j--){
highscores[diff - 3][j] = highscores[diff - 3][j - 1];
players[diff - 3][j] = players[diff - 3][j - 1];
}
highscores[diff - 3][i] = score;
players[diff - 3][i] = player;
break;
}
}
}
为什么没报错,按理说final修饰后应该不能修改
------解决方案--------------------
final 修饰一个引用类型,指的是不能对变量重新赋值,但变量本身可以变化。
------解决方案--------------------
public final static int[][] highscores = new int[3][5];
public final static String[][] players = new String[][]{
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
};
这的final修饰的是 highscores这个变量 ,它指向的内存地址不能变,但是他内存地址存放的值是可以改变的
------解决方案--------------------
比如
public final static int[][] highscores = new int[3][5];
public void xXXX(){
int [][] A = new int[3][5] ;
...
highscores = A ; //这样子是错误的,highscores 指向的对象在内存中的地址不能改变
highscores[0][0] = 1 ;//合法 ,赋值
}