“从内部类引用的局部变量必须是最终的或有效的最终变量” - 如何解决?

我见过有类似问题的其他线程,但是在这些线程中,解决方案是创建一个带有值副本的最终变量,以便该变量实际上是最终的。但是,我的问题源于这样一个事实,即产生错误的变量是一个包含类实例的二维数组。


这是发生错误的 Controller 类的代码:


package Controller;


import Model.Die;

import View.GameWindow;

import View.HelpWindow;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;


public class Controller {


private GameWindow view;

private static int pos[] = new int[2];

private Die diceLayout[][];

private String createdWord;


public Controller(){


    view = new GameWindow();

    addHelpListener();

    addSubmitListener();

    diceLayout = view.getDice();


}


private void submitWord(String word){

    boolean valid = checkValidWord(word);

    if(valid){

        System.out.println("The word ‘"+word+"‘ is valid.");

    }else{System.out.println("The word ‘"+word+"‘ is not valid.");}

}


private boolean checkValidWord(String word){

    boolean validSpell = validWordDic(word);

    boolean connected = checkWordConnected(word);

    if(validSpell&&connected){

        return true;

    }else{

        return false;

    }

}


private static boolean validWordDic(String word){

    word=word.toLowerCase();

    try {

        BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\sambe\\Desktop\\IPT\\BoggleGame\\res\\dictionary.txt"));

        String str;

        while ((str = in.readLine()) != null) {

            if (str.indexOf(word) != -1) {

                return true;

            }

        }

        in.close();

    } catch (IOException e) {

        e.printStackTrace();

    }


    return false;

}


public boolean checkWordConnected(String word){

    word = word.toLowerCase();

    String letters[] = word.split("");

    for(int i = 0; i<4; i++){

        for(int j = 0; j<4; j++){

            if(letters[0]==diceLayout[i][j].getText()){

                pos[0]=i;

                pos[1]=j;

            }

        }

    }

    return true;

}


如果有人对我如何解决此问题有任何建议,将不胜感激。谢谢!


噜噜哒
浏览 1060回答 2
2回答

临摹微笑

这不是关于数组,而是关于循环计数器。由于i和j不是最终的(它们由循环递增),因此它们不能在匿名类中使用。diceLayout[i][j].setClicked(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^&nbsp; &nbsp; &nbsp; &nbsp;error here (and if you fixed that, it would appear at j)您可以将 提取Die到局部变量中,因此只能访问i和j在侦听器之外:private void addDiceListeners(){&nbsp; &nbsp; for (int i = 0; i < 4; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 4; j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Die die = diceLayout[i][j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die.addActionListener(new ActionListener()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die.setClicked(true);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

斯蒂芬大帝

这里:diceLayout[i][j].setClicked(true); //Error Occurs here你的问题是 i 和 j。这些是来自封闭方法的局部变量。当然:那些是循环计数器,因此您不能将它们设为最终。这应该做:for(int i = 0; i<4; i++) {&nbsp; &nbsp; for(int j = 0; j<4; j++) {&nbsp; &nbsp; &nbsp; final int finalRow = i;&nbsp; &nbsp; &nbsp; final int finalColumn = j;然后使用您刚刚创建的两个最终副本而不是 i 和 j。或者,您按照另一个答案的建议进行操作并获取Die要使用的实际对象(作为最终对象)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java