简体   繁体   中英

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. I'm using NetBeans, and the program is going to be a 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):

'void' type not allowed here

I'm fairly new to Java, so still learning.

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:

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:

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.

Your method should return a String if you want to add a String in a ArrayList of 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.
A QuestionAndResponse could contain the text of the question but also the text of the expected response.

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 <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 :

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");
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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