【问题标题】:Find the index in an ArrayList that contains a string在包含字符串的 ArrayList 中查找索引
【发布时间】:2013-02-05 01:59:54
【问题描述】:

通过使用 Jsoup,我从网站解析 HTML 以填充 ArrayList,其中包含我需要从网站获取的内容。所以现在我有一个充满字符串的ArrayList。我想在该列表中找到包含某个字符串的索引。例如,我知道在列表中的某个位置,在某个索引中,有字符串(文字)“Claude”,但我似乎无法编写任何代码来找到 @987654324 中 contains“Claude”的索引@...这是我尝试过但返回-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);
        }
    }
}

【问题讨论】:

  • Claude 是更大字符串的一部分,还是列表中的一个字符串?
  • 尝试打印字符串a 并检查“Claude”。它不应该在那里。研究如何使用 JSoup 迭代 html 标签
  • 如果将“Claude”添加到列表中,我看不到任何获得 -1 的理由。插入时注意多余的空格,可以在插入前使用修剪。大小写也很重要,“克劳德”与“克劳德”不同。
  • 从您的代码来看,您将需要遍历 ArrayList,逐个元素地对每个元素执行 String#contains
  • 好吧.. 和 Rohit Jain - Claude 是一个更大的字符串的一部分。

标签: java string arraylist jsoup indexof


【解决方案1】:

您混淆了String.indexOfList.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

所以:

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);
  }
}

【讨论】:

    【解决方案2】:
    First check whether it is an instance of String then get index
    
    if (x instanceof String) {
        ...
    }
    
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getX() == someValue) { // Or use equals() if it actually returns an Object.
            // Found at index i. Break or return if necessary.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      相关资源
      最近更新 更多