java将列表添加到列表列表中不起作用
我无法使此代码正常工作。 这个从中读取的txt文件如下: urls.txt
这段代码的问题是它根本没有添加任何行。 每当我试图从“列表”中得到任何东西时,它都会给我一种IndexOutOfBoundsException的感觉
代码每30秒执行一次,所以我必须调用列表。清除();方法
注:“列表”在前面有定义:
public static ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
这是我的代码:
try {
URL urls = new URL("https://dl.dropboxusercontent.com/u/22001728/server%20creator/urls.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(urls.openStream()));
lists.clear();
String line;
ArrayList<String> list = new ArrayList<String>();
lists.add(list);
int y = 0, z = 0;
while ((line = br.readLine()) != null) {
y++;
if(line.equalsIgnoreCase(">")) {
System.out.print("Received command: INSERT ");
lists.add(list);
list.clear();
z++;
System.out.print(" ; Jumping to " + z + "\n");
} else {
System.out.println("Line: " + line + " added to lists[" + z + "].");
lists.get(z).add(line);
}
}
int selectedIndex = comboModel0.getIndexOf(comboModel0.getSelectedItem());
comboModel0.removeAllElements();
System.out.println("\n\n\n");
System.out.println(lists.get(1).get(1));
System.out.println("\n\n\n");
} catch (IOException e) {
System.out.println("Failed to download URLS data.");
}
# 1 楼答案
您正在向列表集合中添加一个列表,然后将其清除。这将清除局部变量,以及集合中的列表
您需要创建该列表的副本以添加到收藏中