I can not get this code to work. The .txt file that it is reading from is here: urls.txt
The problem with this code is that it does not at all add any lines. Whenever i try to get anything at all from "lists" it gives me an IndexOutOfBoundsException.
The code gets executed each 30 seconds, so i have to call the lists.clear(); method.
NOTE: "lists" is defined earlier:
public static ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
This is my code:
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.");
}
You are adding a list to your collection of lists, then clearing it. That will clear your local variable, plus the list in your collection.
lists.add(list);
list.clear();
You need to create a copy of that list to add to your collection.
lists.add(new ArrayList<String>(list));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.