Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList类型未定义-将ArrayList的随机对象放入变量_Java_Variables_Arraylist_Types_Undefined - Fatal编程技术网

Java ArrayList类型未定义-将ArrayList的随机对象放入变量

Java ArrayList类型未定义-将ArrayList的随机对象放入变量,java,variables,arraylist,types,undefined,Java,Variables,Arraylist,Types,Undefined,我试图将随机选择的ArrayList对象放入类构造函数中的变量中 public WordsToGuess randomWord() { int index = randomGenerator.nextInt(words.size()); WordsToGuess chosenWord = words.get(index); return chosenWord; } public Model() throws IOException

我试图将随机选择的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();
        }
chosenWord = words.randomWord();
public words猜测随机词()
{
int index=randomGenerator.nextInt(words.size());
WordsToGuess chosenWord=words.get(索引);
返回chosenWord;
}
公共模型()引发IOException
{
words=newarraylist();
chosenWord=words.randomWord();
随机生成器=新随机();
}
我得到一个错误,说“类型ArrayList的方法randomWord()未定义”。我已经从构造函数中删除了不必要的代码

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();
        }
chosenWord = words.randomWord();
应改为:

chosenWord = randomWord();
randomWord()
不是ArrayList的方法,而是您的类的方法。

您的
randomWord()
方法似乎是本地实例方法。请尝试以下方法:

chosenWord = randomWord();

我会补充说,你的设计可能需要一些整理。您的方法似乎有一个混乱的目的:尽量使每个方法的任务尽可能小和集中。

“类型不匹配=无法从单词猜测转换为字符串”:(chosenWord应该是Words类型的对象而不是字符串。这已经修复了错误。我将整理出我遇到的另一个错误,希望没有运行时错误。感谢您的帮助!如果您的答案中有一个对您有帮助,无论是我的还是波西米亚人,请接受它。很快就要解决了。:)正在检查其他事情。“类型不匹配=无法从WordsToGuess转换为字符串”:(我的意思是,即使在那时..代码也是乱七八糟的,因为它什么都不会做。
单词
存储的是什么类型?它都是hangman应用程序的一部分。我删除了很多其他代码。这是与发生的错误相关的代码。从文件加载字符串。为什么是-1?Anubian设法解决了它,所以它并没有真正脱离主题。。。