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

编程入门 行业动态 更新时间:2024-10-17 23:29:15
本文介绍了Java ArrayList.add()用最终元素替换数组中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经检查了该站点,似乎其他人也遇到了类似的问题,但是他们的问题是由于静态字段引起的.我有2个班级,一个是Questions类,另一个是Questions类的ScienceQuestions类:

I've checked through this site and it seems other people have had similar problems but theirs were due to static fields. I have 2 classes, a Questions class and a ScienceQuestions class which extends Questions:

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中的所有元素都与我添加的最后一个元素相同.为什么会这样?

Now when I instantiate the ScienceQuestions class all of the elements in the ArrayList sciQuestions are of the same as the final element the I've added. Why is that?

推荐答案

您仅创建一个实例并覆盖属性.

You create only one instance and overwrite the properties.

您可以:

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并替换

and in ScienceQuestions.class remove the extends Questions and replace

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

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

更多推荐

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

本文发布于:2023-11-06 23:48:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   组中   Java   ArrayList   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!