我试图将随机选择的ArrayList对象放入类构造函数中的变量中。
public WordsToGuess randomWord()
{
int index = randomGenerator.nextInt(words.size());
WordsToGuess chosenWord = words.get(index);
return chosenWord;
}
public Model() throws IOException
{
words = new ArrayList<WordsToGuess>();
chosenWord = words.randomWord();
randomGenerator = new Random();
}
我收到一条错误消息“方法randomWord()未定义类型ArrayList”。我从构造函数中删除了不必要的代码。
答案 0 :(得分:0)
chosenWord = words.randomWord();
应阅读:
chosenWord = randomWord();
randomWord()
不是ArrayList的方法,而是你的类的方法。
答案 1 :(得分:0)
您的randomWord()
方法似乎是本地实例方法。试试这个:
chosenWord = randomWord();
我想补充一点,你的设计可能需要整理一下。看来你的方法有一个混乱的目的:尽量保持每个方法的任务尽可能小和集中。