2016-04-13 2 views
0

Ich habe diese Website überprüft und es scheint, dass andere Leute ähnliche Probleme hatten, aber ihre waren aufgrund statischer Felder. Ich habe 2 Klassen, eine Fragen-Klasse und eine ScienceQuestions Klasse, die Fragen erweitert:Java ArrayList.add() ersetzt alle Elemente im Array mit dem letzten Element

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

Nun, wenn ich die ScienceQuestions Klasse alle Elemente in der Arraylist sciQuestions instanziiert sind von der gleichen Als letztes Element habe ich hinzugefügt. Warum das?

Antwort

1

Sie erstellen nur eine Instanz und die Eigenschaften überschreiben.

Sie könnten:

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

und in ScienceQuestions.class die extends Questions entfernen und ersetzen

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

mit

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

Danke, es funktioniert. Ich vermute also, das erste Mal, dass es nicht funktionierte, war, weil ich keine neuen Questions() instanziiert hatte, so dass addQuestion() nur eine "Instanz" dieser Klasse betraf? – user3544582

+0

Genau: Sie haben eine Instanz/ein Objekt erstellt und dieses Objekt zur Arraylist hinzugefügt. Aber es ist Referenz, also werden keine neuen Objekte erstellt. Während der addQuestion-Methode haben Sie die Eigenschaften des Objekts geändert, und wenn Sie get (index) für die Arraylist aufrufen, erhalten Sie für jeden Index das gleiche Objekt, also die gleichen Parameter, die Sie zuletzt gesetzt haben. –

0

Ich könnte falsch liegen, aber ich denke, das Problem könnte sein, dass Sie die Arraylist mit einem No-Parameter-Konstruktor instanziieren. Dies kann eine Arraylist mit einer Größe von 1 instanziieren. Add fügt einfach an das Ende der Liste an (und erhöht nicht die Kapazität). Vielleicht sollten Sie es mit einer Größe instanziiert wie hier beschrieben:

Constructor with initial capacity

edit: änderte die Wortgröße Kapazität

+0

das nicht der Fall, da die Größe sein kann meiner Arraylist ist immer noch 3 – user3544582

Verwandte Themen