【发布时间】:2016-08-12 04:26:12
【问题描述】:
注意:有一个类似的问题被问到HERE。但是,我已经审查了这个问题,它并没有解决我的问题。请提前阅读。
我编写了一个尝试从Elements 中删除某些链接的方法。我知道remove() 会从它所在的Document ob 中删除Element。但是,如何更新我的 Elements 使其不包含已删除的链接?
下面是我的方法。
public void getLinks(Document site) {
Elements links = site.select("a[href]");
for(int i = 0 ; i < links.size() ; i++) {
String url = links.get(i).attr("abs:href");
if(url.endsWith("~S1")) {
System.out.println(url);
} else {
links.remove(i); // links still contains removed Element
}
}
}
【问题讨论】:
-
当心!事实上,循环:
for (int i = 0; i < 0; i++),永远不会发生,因为i < 0在开始循环之前返回false... -
你既不能使用 i0 ,如果你使用 i>0 你将有一个无限循环。而不是 0 使用另一个整数值。因为你不能在for循环中反复检查起始值的条件。
-
感谢您的指点 :) 但是,我的问题仍然存在。还有其他想法吗?