简体   繁体   English

我如何通过 ArrayList<string> 在 Java 中从一种方法到另一种方法。 变量未初始化错误</string>

[英]How do I pass an ArrayList<String> from one method to another in Java. Variable not initialised error

New to Java coding, please bear with me. Java编码新手,请多多包涵。 I'm trying to pass my ArrayList chosenWords held in my method getChosenWords(int length) to my method getRandom(int length).我正在尝试将我的方法 getChosenWords(int length) 中保存的 ArrayList selectedWords 传递给我的方法 getRandom(int length)。 I need the ArrayList so I can chose a random index and return a random string.我需要 ArrayList 所以我可以选择一个随机索引并返回一个随机字符串。

Note, it's not declared in the Class fields and I am not able to change the parameters of my methods .请注意,它没有在 Class 字段中声明,我无法更改方法的参数 I have to find another way to pass the ArrayList in but I'm unsure how to.我必须找到另一种方法来传递ArrayList但我不确定如何去做。 The unit test uses these set parameters and I can't change parameters or the fields as other classes rely on them staying the same.单元测试使用这些设置参数,我无法更改参数或字段,因为其他类依赖它们保持不变。

I'm pretty sure this is the line that needs changing ArrayList<String> chosenWords;我很确定这是需要更改ArrayList<String> chosenWords;的行and currently I have an error variable not initialised on this line.目前我有一个错误变量未在此行初始化 random.nextInt(chosenWords.size());

Any suggestions?有什么建议么?

Method1方法1

// Takes strings in ArrayList words and stores strings with char int length to ArrayList chosenWords

public ArrayList<String> getChosenWords(int length) {
       ArrayList<String> chosenWords = new ArrayList<>(); 
       
       for(String word1 : words) {
            if(word1.length() == length) {
                 chosenWords.add(word1);
            }
       }
       return chosenWords;  
}

Method2方法2

//Returns a randomly selected string from ArrayList chosenWords

public String getRandom(int length) {
    ArrayList<String> chosenWords;
    Random random = new Random();
    int randomIndex = random.nextInt(chosenWords.size());
    return chosenWords.get(randomIndex);        
}

The obvious solution, if the OP hasn't used it yet?显而易见的解决方案,如果 OP 还没有使用它?

public String getRandom(int length) {
    ArrayList<String> chosenWords = getChosenWords(length);
    Random random = new Random();
    int randomIndex = random.nextInt(chosenWords.size());
    return chosenWords.get(randomIndex);        
}

That way chosenWords will be initialized.这样, selectedWords 将被初始化。

Instead of receiving length as a parameter in the getRandom() function, take an ArrayList.不要在getRandom() function 中接收length作为参数,而是采用 ArrayList。 Pass the arraylist chosenWords to the getRandom() function and return the value received from the getRandom() function.chosenWordsgetRandom() function 并返回从getRandom() function 接收到的值。

Hope that helps.希望有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串变量 java 从一种方法传递到另一种方法 - How pass string variable java from one method to another method 我无法将一个函数的值传递给Java中的另一个函数。 如何正确执行? - I am not able to pass the values of one function to another function in java. How can I do it correctly ? 如何在Java中将字符串从一个方法传递到另一个方法 - How to pass a string from one method to another method in Java 如何基于Java中对象的一个​​成员变量从数组列表中删除另一个数组列表中存在的对象? - How do I remove objects from an arraylist which are present in another arraylist based on one member variable of the object in Java? 如何在Java中将数组列表从一个类复制到另一个类? - How do I copy an arraylist from one class to another in Java? 我想将三个不同的字符串传递给java中的main方法。 我该怎么做呢? - I want to pass three different strings to my main method in java. How do I do this? 如何在Java中将变量从一类传递到另一类? - How Do You Pass a Variable from One Class to Another in Java? 我如何将变量从一个jframe传递到另一个jframe - how do i pass variable from one jframe to another jframe 如何将int值从一种方法传递给另一种方法 - How do I pass int values from one method to another 如何将字符串Arraylist从一个活动传递到另一个活动? - How to pass a string Arraylist from one activity to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM