gpt4 book ai didi

java - jsoup:向后遍历 Element 类型的对象

转载 作者:行者123 更新时间:2023-12-02 03:18:57 24 4
gpt4 key购买 nike

Connection con = Jsoup.connect(https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8); //Just giving an example
Document htmlDoc = con.get();
Elements linksOnPage = htmlDoc.select("a[href]");
for(Element link : linksOnPage){
//pushing the links on to a stack
}

我需要的是,我想以这样的方式推送检索到的链接,即从 linksOnPage 检索到的第一个链接成为我的堆栈顶部。

1)有人可以建议我是否有其他方法可以向后遍历链接对象?

2)还有其他方法吗?例如,如果我将所有这些链接按到达顺序复制到 LinkedList,然后向后遍历列表会怎样?我知道这可能是一个有点幼稚的方法。

最佳答案

由于 Elements 是一个列表,因此您可以这样做 -

if (linksOnPage.size() > 0) {
for (int i = linksOnPage.size() - 1; i >=0; i--) {
Element e = linksOnPage.get(i);
//push e
}
}

关于java - jsoup:向后遍历 Element 类型的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39881475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com