简体   繁体   English

数据没有在java中正确存储到arraylists的arraylist

[英]data not storing to arraylist of arraylists properly in java

I'm having issues with trying to store data in an arraylist of arraylists. 我在尝试将数据存储在arraylists的arraylist中时遇到问题。 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. 数组列表将包含从sql db中提取的任务信息列表,一次一行。 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) 从那里我有它拉入任务数据并将其存储到列表列表中(为了节省空间我将截断它,一个pastebin将在底部)

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: 它会提取其余的rs数据,并根据需要将int转换为字符串:

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); 起初我虽然好,我的.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. 是错的,我需要切换i和x,但这给了我空列表...所以我不知所措。

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 在开始附近还有一堆其他的sql东西用于其他东西http://pastebin.com/TAHQrAiw

I'm completely lost here, so any help would be greatly appreciated. 我完全迷失在这里,所以任何帮助都会非常感激。 Thanks in advance. 提前致谢。

You need to copy your questsbloat list. 你需要复制你的questsbloat列表。 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM