0

I'm having issues with trying to store data in an arraylist of arraylists. I've been toying around with different methods on how to handle things, and so far this is all I've come up with. And no matter what I try, it keeps overwriting the first list with the second list, AND it puts the second list where it is supposed to be.

The array list is going to contain lists of quest information that is being pulled from a sql db, one row at a time. Since I want this to be dynamic in height, because I don't know how many quests are going to be there at any given time, I have the setup of the list like so:

public static ArrayList<ArrayList<String>> listofquests = new ArrayList<ArrayList<String>>();
public static ArrayList<String> questbloat = new ArrayList<String>();

When it starts out, it goes through the table and finds out how many quests there are, and saves that number. Then it bloats the list with empty lists like so:

for(int j = 0; j < 37; j++){
        questbloat.add(" ");
    }

  for(int i = 0; i< questcount; i++){
        listofquests.add(questbloat);
  }

From there I have it pull in the quest data and store it into the list of lists (To save space I'll truncate this, a pastebin will be at the bottom)

for(int i = 0; i < questcount ; i++){
            rs = UserVerify.stmt.executeQuery("SELECT Started, Complete, Title, MinLevel, MaxLevel, Solo, Timer, TimeLeft, MobName, MobAmount, MobKilled, Collect1, Amount1, Left1, Collect2, Amount2, Left2, Collect3, Amount3, Left3, Collect4, Amount4, Left4, Collect5, Amount5, Left5, Collect6, Amount6, Left6, Gold, XP, Item1, Item2, Item3, Item4, Item5, Item6 FROM Quests_" + Game.Playername + " WHERE QuestID = " + i);

            while(rs.next()){
                Startedint = rs.getInt("Started");
                Completeint = rs.getInt("Complete");
                Title = rs.getString("Title");
                MinLevelint = rs.getInt("MinLevel");//
                MaxLevelint = rs.getInt("MaxLevel");//
                Soloint = rs.getInt("Solo");//
                Timerint = rs.getInt("Timer");//
                TimeLeftint = rs.getInt("TimeLeft");//

it pulls the rest of the rs data, converts ints to Strings as necessary:

Started = Integer.toString(Startedint);
Complete = Integer.toString(Completeint);
MinLevel = Integer.toString(MinLevelint);
MaxLevel = Integer.toString(MaxLevelint);

Then it starts to store it to the list of lists:

listofquests.get(i).set(0, Started);
listofquests.get(i).set(1,  Complete);
listofquests.get(i).set(2, Title);
listofquests.get(i).set(3, MinLevel);
listofquests.get(i).set(4, MaxLevel);
listofquests.get(i).set(5, Solo);
listofquests.get(i).set(6, Timer);

If I make it so it only needs to add one quest, I get this

 [[1, 0, Ninja Slayer, 1, 100, 1, 0, 0, NinjaBot, 10, 0, a, 1, 0, a, 1, 0, a, 1, 0, a, 1, 0, a, 1, 0, a, 1, 0, 127, 11, a, a, a, a, a, a]] 

but when I let it see two quests I get this

[[0, 0, Ninja Boss, 5, 100, 1, 0, 0, NinjaBotBoss, 1, 0, a, 1, 0, a, 1, 0, a, 1, 0, a, 1, 1, a, 1, 1, a, 1, 0, 127, 55, a, a, a, a, a, a], [0, 0, Ninja Boss, 5, 100, 1, 0, 0, NinjaBotBoss, 1, 0, a, 1, 0, a, 1, 0, a, 1, 0, a, 1, 1, a, 1, 1, a, 1, 0, 127, 55, a, a, a, a, a, a]]

for some reason it goes back over the first row, fills it in, then fills in the second row. At first I though ok, my .get(i).set(x, value); was wrong, and that I needed to switch i and x, but that gave me empty lists ... so I'm at a loss.

here is the full class. there's a bunch of other sql stuff near the start that is for other things http://pastebin.com/TAHQrAiw

I'm completely lost here, so any help would be greatly appreciated. Thanks in advance.

1
  • class Quest {....}, create a natural container for all that data would be my first tip. Commented Dec 1, 2013 at 19:58

1 Answer 1

2

You need to copy your questsbloat list. By using the same reference repeatedly, you are in fact storing the same list of quests in every row in your list of lists

for(int i = 0; i< questcount; i++){
  listofquests.add(new ArrayList<String>(questbloat));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.