Home > Software design >  Is there a way to store ArrayList information when "newing" up a class without it emptying
Is there a way to store ArrayList information when "newing" up a class without it emptying

Time:11-04

In this example, I have a Database class that takes in new Question("Genre", "Question", "ArrayListOfStrings choices, "Answer", "Fun Fact").

What I want is for when I add the choices to a specific question to stay those choices. But when I remove the choices after adding a new Question, the choices switches to the ones that have been added. Is there any way to bypass this?

public static ArrayList<Question> allInitialQuestions(ArrayList<Question> q)
    {

    ArrayList<String> c = new ArrayList<String>();

    // These choices would be added to the choices ArrayList
    c.add("Pacific");
    c.add("Atlantic");
    c.add("Arctic");
    c.add("Indian");

    // Then what is stored in c at this moment should stay there
    // This question should have the aquatic choices
    q.add(new Question("Geography","Which ocean is the largest?", c, "Pacific", "The Pacific Ocean stretches to an astonishing 63.8 million square miles!"));

    // However, when I remove everything on the ArrayList,
    // it automatically updates the other ArrayList to display
    // the ones that have been added to it.
    c.removeAll(c);

    c.add("192");
    c.add("195");
    c.add("193");
    c.add("197");

    q.add(new Question("Geography", "How many countries are in the world?", c, "195", "Africa has the most countries of any continent with 54."));

    c.removeAll(c);

    c.add("Mississippi");
    c.add("Nile");
    c.add("Congo");
    c.add("Amazon");

    q.add(new Question("Geography", "What is the name of the longest river in the world?", c, "Nile","Explorer John Hanning Speke discovered the source of the Nile on August 3rd, 1858."));

    c.removeAll(c);

    c.add("United States");
    c.add("China");
    c.add("Japan");
    c.add("India");

    q.add(new Question("Geography","Which country has the largest population?" ,c, "China", "Shanghai is the most populated city in China with a population of 24,870,895."));

    c.removeAll(c);

    c.add("Mars");
    c.add("Mercury");
    c.add("Venus");
    c.add("Jupiter");

    q.add(new Question("Geography","Which planet is closest to Earth?",c,"Venus","Even though Venus is the closest, the planet it still ~38 million miles from Earth!"));

    c.removeAll(c);

    c.add("Sega");
    c.add("Nintendo");
    c.add("Sony");
    c.add("Atari");

    q.add(new Question("Video Games", "Which company created the famous plumber Mario?", c, "Nintendo", "Nintendo created Mario in 1981 for the arcade game Donkey Kong."));

    c.removeAll(c);

    c.add("Sonic");
    c.add("Tales");
    c.add("Knuckles");
    c.add("Amy");

    q.add(new Question("Video Games", "What is the name of the famous video character who is a blue hedgehog?",c,"Sonic", "In some official concept art, Sonic was originally meant to be a rabbit."));

    c.removeAll(c);

    c.add("Wii Sports");
    c.add("Grand Theft Auto V");
    c.add("Tetris");
    c.add("Minecraft");

    q.add(new Question("Video Games","As of 2022, which of the following is the best selling video game of all time?",c,"Minecraft","As of 2022, Minecraft has sold over 238 million units."));



    return q;
}

CodePudding user response:

Rather than removing all entries, instantiate a new List

c = new ArrayList<String>();
  • Related