我开发的应用程序可以从互联网上获取一些公交车时刻表。
三个班级: MainClass.java, TKLReader.java, Logger.java(仅用于记录)
一个外部图书馆:Jsoup
程序按以下顺序运行: 主---->
-> initApp (inits app)
-> TKLReader.connect (connects to the website)
-> TKLReader.read (reads the information needed and gathers it to list)
-> printTimetable (prints out the timetable)
这是我的问题:
我运行程序,它应该连接。我得到的信息,我可以用我的Logger类登录。当我尝试从列表中打印东西时,我(尝试)将信息存储在List<List<String>>
但是我收到以下错误:
23:20Exception in thread "Thread-1" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at TKLReader.read(TKLReader.java:101)
at MainClass$1.run(MainClass.java:42)
at java.lang.Thread.run(Unknown Source)
我知道列表中有这些项目,因为我可以打印它的大小而不是0.由于某种原因,该程序认为它有0项。
以下是TKLReader.java的代码:
public class TKLReader {
public static Document doc;
public static int num_lines;
public static int num_stops;
public static final boolean debug = false; //For debugging purpose
public void connect(final String url, final int num_lines, final int num_stops) {
//This function connects to the website
TKLReader.num_lines = num_lines;
TKLReader.num_stops = num_stops;
Thread t = new Thread(new Runnable() {
public void run() {
try {
//When Jsoup connects, it sets the doc variable
doc = Jsoup.connect(url).get();
} catch (IOException e) {
Logger.log("TKLHUB", e.getMessage());
}
}
});
t.start();
}
public List<List<String>> read() {
//Initializing variables...
List<List<String>> returnList = new ArrayList<List<String>>();
List<String> tempList = new ArrayList<String>();
Elements tdList;
int counter = 0, lineCounter = 0;
//These int-arrays define the td-elements' indexes on the page. They vary depending if
//the user selects one or more bus lines.
int[] tdIndexesSingle = {
4,6,7,8,9
};
int[] tdIndexesMulti = {
5,7,8,9,10,
12,14,15,16,17,
19,21,22,23,24,
26,28,29,30,31,
33,35,36,37,38
};
//this selects the array to use
int[] tdIndexes = num_lines == 1 ? tdIndexesSingle : tdIndexesMulti;
if(doc == null) return null;
tdList = doc.getElementsByTag("td");
if(tdList.size() == 0) return null;
for(int i = 0; i < tdList.size(); i++) {
String item = tdList.get(i).text();
if(!debug) {
if(contains(tdIndexes, i) && lineCounter < num_lines) {
tempList.add(item); //here I clearly add an item to the tempList, which is later added to the returnList
Logger.log("Added item: " + item); //Logger is a class that I created for logging
counter++; //The tds are in groups of five (number, destination, time1, time2, time3)
//When counter hits 5, the loop starts a new list
if(counter == 5) {
Logger.log("Reset: " + item);
Logger.log("tempList Size: " + tempList.size());
returnList.add(tempList); //Here I add the tempList
tempList.clear(); //and then clear it
counter = 0; // and set the counter back to 0
lineCounter++; //mark that this line has been handled
Logger.log("Size: " + returnList.size()); //I log the size of the returnList
if(returnList.size() != 0) {
//Logger.log(returnList.get(0).get(0)); //Here I get an error, it says Index 0, Size 0
//although I've just added items
}
}
}
} else {
Logger.log("(" + i + ")" + item); //For debugging
}
}
return returnList; //This returns the list
}
public static boolean contains(int[] a, int key) { //Checks whether a specific key is inside an array
for(int i = 0; i < a.length; i++) {
if(a[i] == key) return true;
}
return false;
}
}
Logger.java:
public class Logger {
public static void log(final String tag, final String txt) {
System.out.println(tag + ": " + txt);
}
public static void log(final String txt) {
System.out.println(txt);
}
}
请帮帮我! 如果需要请求澄清!
答案 0 :(得分:1)
问题在于:
returnList.add(tempList); //Here I add the tempList
tempList.clear(); //and then clear it
您不能将tempList
的副本放入returnList
;你将tempList
置于其中。所以当你清除它时,returnList
中的项目(同一个对象)也会被清除。
然后,当你拨打这个电话时:
returnList.get(0).get(0)
您有效地致电
tempList.get(0)
哪个无效,因为tempList
现在为空。
在tempList
循环中移动for
的声明,然后移除clear
。这将在每次迭代时为您提供一个新的空列表,然后可以安全地添加。