在foreach循环中选择特定的项目? [英] Select specific items in foreach loop?

查看:136
本文介绍了在foreach循环中选择特定的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码来获取一些标签:
$ b $ pre $ HttpResponse响应= client.execute(方法);
InputStream stream = response.getEntity()。getContent();
String结果= inputestreamToString(流);
String html = result;
Document doc = Jsoup.parse(html);
元素mElements = doc.select(div [class ^ = news-item]); (Element e:mElements)


{
title = e.text();
jobtitle.add(title);

StructNote note = new StructNote();
note.title = title;
Notes.add(note);






我有100个div,他们。但是我只想得到20个项目。

解决方案

您也可以使用 Iterables#limit from < a href =https://code.google.com/p/guava-libraries/ =nofollow> Guava 。

 {
....
}


I have this code to getting some tags:

HttpResponse response = client.execute(method);
InputStream stream = response.getEntity().getContent();
String result = inputestreamToString(stream);
String html = result;
Document doc = Jsoup.parse(html);
Elements mElements = doc.select("div[class^=news-item]");

for (Element e: mElements) 
{
    title = e.text();
    jobtitle.add(title);

    StructNote note = new StructNote();
    note.title = title;
    Notes.add(note);

}

I have 100 divs and with this loop I can get all of them. But I want to get only 20 items.

解决方案

You can also use Iterables#limit from Guava.

for (Element e : Iterables.limit(mElements, 20)) 
{
    ....
}

这篇关于在foreach循环中选择特定的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆