Max*_*axK 8 java string arraylist indexof jsoup
通过使用Jsoup,我从网站解析HTML以填充ArrayList我需要从网站获取的内容.所以现在我有一个ArrayList充满字符串的东西.我想在该列表中找到包含特定字符串的索引.例如,我知道列表中的某个地方,在某个索引中,有字符串(文字)"Claude",但我似乎无法制作任何代码,找到contains"Claude"中的索引ArrayList...这里是我尝试过但返回-1(未找到):
ArrayList < String > list = new ArrayList < String > ();
String claude = "Claude";
Document doc = null;
try {
doc = Jsoup.connect("http://espn.go.com/nhl/team/stats/_/name/phi/philadelphia-flyers").get();
} catch (IOException e) {
e.printStackTrace();
}
for (Element table: doc.select("table.tablehead")) {
for (Element row: table.select("tr")) {
Elements tds = row.select("td");
if (tds.size() > 6) {
String a = tds.get(0).text() + tds.get(1).text() + tds.get(2).text() + tds.get(3).text() + tds.get(4).text() + tds.get(5).text() + tds.get(6).text();
list.add(a);
int claudesPos = list.indexOf(claude);
System.out.println(claudesPos);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*ica 26
你是混乱String.indexOf和List.indexOf.考虑以下列表:
list[0] = "Alpha Bravo Charlie"
list[1] = "Delta Echo Foxtrot"
list[2] = "Golf Hotel India"
list.indexOf("Foxtrot") => -1
list.indexOf("Golf Hotel India") => 2
list.get(1).indexOf("Foxtrot") => 11
Run Code Online (Sandbox Code Playgroud)
所以:
if (tds.size() > 6) {
// now the string a contains the text of all of the table cells joined together
String a = tds.get(0).text() + tds.get(1).text() + tds.get(2).text() +
tds.get(3).text() + tds.get(4).text() + tds.get(5).text() + tds.get(6).text();
// now the list contains the string
list.add(a);
// now you're looking in the list (which has all the table cells' items)
// for just the string "Claude", which doesn't exist
int claudesPos = list.indexOf(claude);
System.out.println(claudesPos);
// but this might give you the position of "Claude" within the string you built
System.out.println(a.indexOf(claude));
}
for (int i = 0; i < list.size(); i += 1) {
if (list.get(i).indexOf(claude) != -1) {
// list.get(i).contains(claude) works too
// and this will give you the index of the string containing Claude
// (but not the position within that string)
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54678 次 |
| 最近记录: |