简体   繁体   English

将方法放入ArrayList Java

[英]Putting methods into an ArrayList Java

I would like to put these Methods into an ArrayList, but I can't figure out how to do that. 我想将这些方法放到ArrayList中,但是我不知道该怎么做。 I'm using NetBeans, and the program is going to be a GUI. 我正在使用NetBeans,该程序将成为GUI。

Here is my code: 这是我的代码:

public class canada150Tour extends javax.swing.JFrame {

    ArrayList <String> mcQuestions = new ArrayList(); //This is the array I want to put the below methods into.


    private void questionFredricton(){
        outputTextQuestion.append("Where is Fredricton located?");

        displayQuestions.append("New Brunswick");        
    }
    private void questionRegina(){
        outputTextQuestion.setText("Where is Regina located?");

        displayQuestions.append("Saskatchewan");

    }
    private void questionOttawa(){
        outputTextQuestion.setText("Where is Ottawa located?");

        displayQuestions.append("Ontario");
    }
    private void questionHalifax(){
        outputTextQuestion.setText("Where is Halifax located?");

        displayQuestions.append("Nova Scotia");
    }
    private void questionQuebecCity(){
        outputTextQuestion.setText("Where is Quebec City located?");

        displayQuestions.append("Quebec");
    }
    private void questionEdmonton(){
        outputTextQuestion.setText("Where is Edmonton located?");

        displayQuestions.append("Alberta");
    }
    private void questionCharlottetown(){
        outputTextQuestion.setText("Where is Charlottetown located?");

        displayQuestions.append("Prince Edward Island");
    }
    private void questionVictoria(){
        outputTextQuestion.setText("Where is Victoria located?");

        displayQuestions.append("British Columbia");
    }
    private void questionIqaluit(){
        outputTextQuestion.setText("Where is Iqaluit located?");

        displayQuestions.append("Nunavut");
    }
    private void questionWhitehorse(){
        //outputTextQuestion.setText("Where is Whitehorse located?");

        displayQuestions.append("Yukon");
    }
    private void questionWinnipeg(){
        outputTextQuestion.setText("Where is Winnipeg located?");

        displayQuestions.append("Manitoba");
    }

I've tried: 我试过了:

Collections.addAll(mcQuestions, questionFredricton(), questionRegina()); 

And so on for all the methods, but obviously that isn't working. 对于所有方法,依此类推,但显然不起作用。

I get a compiling error when I'm trying to add to the ArrayList (above): 当我尝试添加到ArrayList时,出现编译错误:

'void' type not allowed here 此处不允许使用“ void”类型

I'm fairly new to Java, so still learning. 我是Java的新手,所以仍然在学习。

EDIT: This will eventually be a multiple choice quiz. 编辑:这最终将是一个选择题。 I essentially want to the put the methods into an array so I can chose methods randomly. 我本质上是想将方法放入数组,以便可以随机选择方法。

Thanks in advance! 提前致谢!

You need to pass the ArrayList as an argument to methods in order to add elements inside it, eg: 您需要将ArrayList作为参数传递给方法,以便在其中添加元素,例如:

private void questionFredricton(ArrayList <String> mcQuestions){
    mcQuestions.add("Where is Fredricton located?");
}

And call the method like this: 并调用如下方法:

ArrayList <String> mcQuestions = new ArrayList();
questionFredricton(mcQuestions);

Also, it seems you need to store the pair of questions and answers here, in which case, it's better to use HashMap , eg: 另外,似乎您需要在此处存储一对问题和答案,在这种情况下,最好使用HashMap ,例如:

private void questionFredricton(Map<String, String> questions){
    questions.put("Where is Fredricton located?", "New Brunswick");
}

And call it like this: 并这样称呼它:

Map<String, String> questions = new HashMap<>();
questionFredricton(questions);

This way, you will be able to store both questions and answers. 这样,您将能够存储问题和答案。 questions.keySet() will give you list of all the questions and questions.get("<questions>") will give you the answer. questions.keySet()将为您提供所有问题的列表,而questions.get("<questions>")将为您提供答案。

Your method should return a String if you want to add a String in a ArrayList of String . 如果要在StringArrayList中添加String则方法应返回String
But in fact I don't think that you should do it because you needs both Question and Response. 但实际上我不认为您应该这样做,因为您既需要问题,也需要响应。 It seems better to have a List of QuestionAndResponse rather than two List of String. 最好有一个QuestionAndResponse列表,而不是两个String List
A QuestionAndResponse could contain the text of the question but also the text of the expected response. QuestionAndResponse可以包含QuestionAndResponse的文本,也可以包含预期响应的文本。

public class QuestionAndResponse{
     public QuestionAndResponse(String question, String response){
         this.question = question;  
         this.response = response;
     }
     // getters for fields
}

So you could declare the List : 因此,您可以声明List:

List <QuestionAndResponse> mcQuestionsResponses = new ArrayList<>(); 

And you could replace this : 您可以替换为:

private void questionFredricton(){
    outputTextQuestion.append("Where is Fredricton located?");
    displayQuestions.append("New Brunswick");        
}

by: 通过:

private QuestionAndResponse questionFredricton(){
    return new QuestionAndResponse("Where is Fredricton located?","New Brunswick");
}

Now you can have a Canada150Tour (note the uppercase for the first letter to follow the naming conventions) constructor that uses them : 现在,您可以拥有一个使用它们的Canada150Tour(注意遵循命名约定的第一个字母为大写):

public class Canada150Tour extends JFrame {
    List <QuestionAndResponse> mcQuestionsResponses;
       ...
    public Canada150Tour(){
         mcQuestionsResponses = new ArrayList<>(); 
         mcQuestionsResponses.add(questionFredricton());
    }

     private QuestionAndResponse questionFredricton(){
        return new QuestionAndResponse("Where is Fredricton located?",
                                        "New Brunswick");
     }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM