Java列表有项目,但没有';I don’我不让我接触它们
我正在开发一个应用程序,可以从互联网上获取一些公交车时刻表 三类: MainClass.java, TKLReader.java, java(仅用于日志记录) 一个外部图书馆: 程序按以下顺序运行: 主要--> 我的问题是: 我运行这个程序,它可以正常连接。我得到了信息,我可以用我的Logger类记录它。我(尝试)将信息存储在Java列表有项目,但没有';I don’我不让我接触它们,java,arrays,list,exception,Java,Arrays,List,Exception,我正在开发一个应用程序,可以从互联网上获取一些公交车时刻表 三类: MainClass.java, TKLReader.java, java(仅用于日志记录) 一个外部图书馆: 程序按以下顺序运行: 主要--> 我的问题是: 我运行这个程序,它可以正常连接。我得到了信息,我可以用我的Logger类记录它。我(尝试)将信息存储在列表中但当我尝试打印列表中的内容时,出现以下错误: 23:20Exception in thread "Thread-1" java.lang.IndexOutOfBoun
列表中
但当我尝试打印列表中的内容时,出现以下错误:
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;
}
}
请帮帮我!
如果需要,请要求澄清 问题在于:
returnList.add(tempList); //Here I add the tempList
tempList.clear(); //and then clear it
您没有将tempList
的副本放入返回列表
;您正在将圣殿骑士自身放入。因此,当您清除它时,returnList
中的项目(即相同的对象)也会被清除
然后,当您拨打此电话时:
returnList.get(0).get(0)
你实际上是在打电话
tempList.get(0)
这是无效的,因为templast
现在为空
将templast
的声明移动到for
循环中,然后移除清除。这将在每次迭代时为您提供一个新的空列表,然后可以安全地添加该列表。请不要发布所有代码。只需发布相关部分。非常感谢!这有帮助。
returnList.get(0).get(0)
tempList.get(0)