Java ArrayList.add()用最终元素替换数组中的所有元素

时间:2016-04-13 10:04:43

标签: arraylist

我已经查看了这个网站,似乎其他人也有类似的问题,但他们的问题是由于静态字段。我有2个类,一个Questions类和一个扩展问题的ScienceQuestions类:

import java.util.*;

public abstract class Questions {

public int questionID;
public int difficulty;
public boolean bAsked;
public String question;
public ArrayList<String> answers;
public String correctAnswer;

public Questions addQuestion(int id, int dif, boolean asked, String q, ArrayList<String> ans, String correct) 
{
    questionID = id;
    difficulty = dif;
    bAsked = asked;
    question = q;
    answers = ans;
    correctAnswer = correct;
    return this;
}

}

import java.util.ArrayList;

public class ScienceQuestions extends Questions 
{
public ArrayList<Questions> sciQuestions = new ArrayList<Questions>();

//Add default questions
public ScienceQuestions()
{       
    sciQuestions.add(0, addQuestion1());
    sciQuestions.add(1, addQuestion2());
    sciQuestions.add(2, addQuestion3());
    System.out.println(sciQuestions.get(0).question + " " + sciQuestions.get(1).question + " " + sciQuestions.get(2).question);
}

//question 1
private Questions addQuestion1()
{
    int questionID = 1;
    int dif = 1;
    boolean asked = false;
    String question = "What is the chemical symbol for Magneisum?";
    ArrayList<String> answers = new ArrayList<>();
    String answer1 = "Mg", answer2 = "M", answer3 = "mg", answer4 = "MG";
    String correctAnswer = answer1;
    answers.add(answer1);
    answers.add(answer2);
    answers.add(answer3);
    answers.add(answer4);
    Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer);
    return thisQ;
}

//question 2
private Questions addQuestion2()
{
    int questionID = 1;
    int dif = 2;
    boolean asked = false;
    String question = "What is the most accurate acceleration on Earth due to gravity as sea level?";
    ArrayList<String> answers = new ArrayList<>();
    String answer1 = "9.81 m/s", answer2 = "9.81 N/Kg", answer3 = "10 m/s^2", answer4 = "10 N/Kg";
    String correctAnswer = answer2;
    answers.add(answer1);
    answers.add(answer2);
    answers.add(answer3);
    answers.add(answer4);
    Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer);
    return thisQ;
}

//question 2
private Questions addQuestion3()
{
    int questionID = 1;
    int dif = 2;
    boolean asked = false;
    String question = "What is the order of magnitude of the gravitational constant in standard units?";
    ArrayList<String> answers = new ArrayList<>();
    String answer1 = "*10^-10", answer2 = "*10^-13", answer3 = "*10^-12", answer4 = "*10^-11";
    String correctAnswer = answer4;
    answers.add(answer1);
    answers.add(answer2);
    answers.add(answer3);
    answers.add(answer4);
    Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer);
    return thisQ;
}
}

现在,当我实例化ScienceQuestions类时,ArrayList sciQuestions中的所有元素都与我添加的最终元素相同。那是为什么?

2 个答案:

答案 0 :(得分:1)

您只创建一个实例并覆盖属性。

你可以:

import java.util.*;

public class Questions {

    public int questionID;
    public int difficulty;
    public boolean bAsked;
    public String question;
    public ArrayList<String> answers;
    public String correctAnswer;

    public Questions(int id, int dif, boolean asked, String q, ArrayList<String> ans, String correct) {
        questionID = id;
        difficulty = dif;
        bAsked = asked;
        question = q;
        answers = ans;
        correctAnswer = correct;
    }
}

并在ScienceQuestions.class中删除extends Questions并替换

addQuestion(questionID, dif, asked, question, answers, correctAnswer);

new Questions(questionID, dif, asked, question, answers, correctAnswer);

答案 1 :(得分:0)

我可能错了,但我认为问题可能是您使用无参数构造函数实例化arraylist。这可能会实例化一个大小为1的arraylist。添加只是附加到列表的末尾(并不会增加容量)。也许你应该用这里描述的大小来实例化它:

Constructor with initial capacity

编辑:将字大小更改为容量