在foreach循环中选择特定的项目? [英] Select specific items in foreach loop?
问题描述
我有这个代码来获取一些标签: 我有100个div,他们。但是我只想得到20个项目。
$ 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);
您也可以使用 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屋!