简体   繁体   English

Java:如何将String添加到对象Arraylist?

[英]Java: How to add String to an object Arraylist?

Harry Potter Which Character Are You Quiz. 哈利·波特,你是哪个角色的测验。

So I want to take each option, and the character(s) associated with each option (below the options is each character(s) separated by a comma ex. 1.Acid Pops Neville Longbottom, 3. Bertie Bott's Every Flavour Beans Luna Lovegood Nymphadora Tonks....I want to send the option, character(s) in the main method and send it as a String to the Question class and store it in an Arraylist of type Answer not String. And in the Answer class will be passed only what the user selects so for ex. 2. Sherbert Lemons and Albus Dumbledore. 因此,我想选择每个选项,并与每个选项相关联的字符(在选项下方是用逗号分隔的每个字符。1.Acid Pops Neville Longbottom,3。Bertie Bott的Every Flavor Beans Luna Lovegood Nymphadora Tonks ....我想在主方法中发送选项,字符并将其作为字符串发送给Question类,并将其存储在Answer不是String类型的Arraylist中。在Answer类中将是仅通过了用户在示例2中选择的内容。Sherbert Lemons和Albus Dumbledore。

String[] questions =
{
    "Favourite sweet",
};
String [][] options =
{
    {"1.Acid Pops","2.Sherbert Lemons","3.Bertie Bott's Every Flavour Beans", 
    "4.Cake","5.Hagrid's Rock Cakes","6.Chocolate Frogs","7.Ginger Newt",
    "8.I hate sweets\n"},

    {"Neville Longbottom","Albus Dumbledore", "Luna Lovegood, Nymphadora 
    Tonks", "Dobby, Arthur Weasley", "Rubeus Hagrid",
    "Harry Potter, Ron Weasley", "Minerva McGonagall, Hermione Granger, 
     Dolores Umbridge, Sybill Trelawney", 
    "Severus Snape, Tom Riddle, Sirius Black, Bellatrix Lestrange, Draco 
     Malfoy, Lucius Malfoy"}
    };

Here is my for loop in my main method: 这是我主要方法中的for循环:

for (int i =0; i < questions.length; i ++){
        String aQuestion = questions[i];
        Question q = new Question(aQuestion);
        System.out.println(aQuestion);
        for(int j=0; j < options[i].length; j++){
            q.setAnswers(options[i][j], options[i+1][j]);
            System.out.println(options[i][j]);
        }
    }

Here's my Question class: 这是我的Question类:

 private String question;
 public ArrayList<Answer> answers = new ArrayList<Answer>();

 public Question(String q){
    question = q;
 }

public void setAnswers(String options, String options2){
    answers.add(options);
    answers.add(options2);
} 

Sorry if this doesn't make sense. 抱歉,如果这没有意义。 I also don't understand why you have an arraylist of type: Answer that matches a class name you have in your program. 我也不明白为什么会有类型为arraylist的答案:与程序中具有的类名匹配的答案。 If that also makes sense. 如果那也有意义。

Here is the problem, You cannot add anything(any other object) to your List<Answer> except for Answer objects. 这是问题所在,除了Answer对象之外,您无法将任何其他对象(任何其他对象)添加到List<Answer>

See the following lines Line 1 and Line 2 参见以下第一行和第二行

public void setAnswers(String options, String options2){
    answers.add(options);    //LINE 1
    answers.add(options2);   //LINE 2
} 

Now, I am not really sure how your Answer really looks like. 现在,我不确定您的Answer到底是什么样子。 But if it has got something to do with the options. 但是,这是否与选择有关。 It can have the following constructor in Answer class Answer类中可以具有以下构造函数

Answer(String option){
   this.option = option;
}

And you can modify your code to look something like this 您可以修改您的代码,使其看起来像这样

public void setAnswers(String options, String options2){
    answers.add(new Answer(options));    //LINE 1
    answers.add(new Answer(options));   //LINE 2
} 

I also don't understand why you have an arraylist of type 我也不明白为什么你有一个类型的数组列表

Collections in java were made typed to avoid accidental addition of wrong object to it. 在Java中对集合进行类型化,以避免意外向其中添加错误的对象。 You can very well omit the type from the declaration but that is not recommended. 您可以很好地从声明中省略类型,但是不建议这样做。

As an alternative solution you can either: 作为替代解决方案,您可以:

  1. Change the type of ArrayList<Answer> answers to more generic one like Serializable or, ArrayList<Answer> answers的类型更改为更通用的ArrayList<Answer> answers ,例如Serializable
  2. Wrap String options1 and options2 and convert them into Answer type and then add them to list. 包装String options1options2并将它们转换为Answer类型,然后将它们添加到列表中。

I'll prefer second option. 我更喜欢第二种选择。

Convert: Create a method in Answer class which return Answer object, you can make it static 转换:在Answer类中创建一个返回Answer对象的方法,可以将其设为静态

public static Answer getAnswerFromString(String answer) {
  // Logic to create Answer object from string
}

Then modify this method as below to add answer to the list: 然后按如下所示修改此方法以将答案添加到列表中:

public void setAnswers(String options, String options2){
  answers.add(Answer.getAnswerFromString(options));
  answers.add(Answer.getAnswerFromString(options2));
}

Or you can add a constructer with String parameter if it make sense as pointed in other answer. 或者,如果其他答案中指出的有意义,则可以添加带有String参数的构造方法。

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

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