Java 在包含字符串的ArrayList中查找索引

Java 在包含字符串的ArrayList中查找索引,java,string,arraylist,jsoup,indexof,Java,String,Arraylist,Jsoup,Indexof,通过使用Jsoup,我从网站解析HTML,用从网站获取的内容填充ArrayList。现在我有了一个ArrayList,它充满了字符串。我想在列表中找到包含某个字符串的索引。例如,我知道列表中的某个地方,在某个索引中,有字符串(文字)“Claude”,但我似乎无法生成任何代码来查找在数组列表中包含的索引“Claude”。。。以下是我尝试过但返回的-1(未找到): ArrayListlist=newarraylist(); 字符串claude=“claude”; 单据单据=空; 试一试{ doc=J

通过使用Jsoup,我从网站解析HTML,用从网站获取的内容填充
ArrayList
。现在我有了一个
ArrayList
,它充满了字符串。我想在列表中找到包含某个字符串的索引。例如,我知道列表中的某个地方,在某个索引中,有字符串(文字)“Claude”,但我似乎无法生成任何代码来查找
数组列表中包含的
索引“Claude”
。。。以下是我尝试过但返回的
-1
(未找到):

ArrayListlist=newarraylist();
字符串claude=“claude”;
单据单据=空;
试一试{
doc=Jsoup.connect(“http://espn.go.com/nhl/team/stats/_/name/phi/philadelphia-flyers).get();
}捕获(IOE异常){
e、 printStackTrace();
}
对于(元素表:doc.select(“table.tablehead”)){
对于(元素行:table.select(“tr”)){
元素tds=行。选择(“td”);
如果(tds.size()>6){
字符串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));
列表.添加(a);
int claudesPos=list.indexOf(claude);
系统输出打印LN(claudesPos);
}
}
}
首先检查它是否是字符串的实例,然后获取索引
if(字符串的x实例){
...
}
对于(int i=0;i
您混淆了
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
因此:

if(tds.size()>6){
//现在字符串a包含所有连接在一起的表格单元格的文本
字符串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));
//现在,列表包含字符串
列表.添加(a);
//现在您正在查看列表(其中包含所有表格单元格的项)
//因为只有字符串“Claude”不存在
int claudesPos=list.indexOf(claude);
系统输出打印LN(claudesPos);
//但这可能会给出“Claude”在您构建的字符串中的位置
系统输出println(a.indexOf(claude));
}
对于(int i=0;i
克劳德是较大字符串的一部分,还是列表中的字符串本身?请尝试打印字符串
a
,并检查是否有“克劳德”。它不应该在那里。研究一下如何使用JSOUP迭代html标记,如果列表中添加了“Claude”,我看不出得到-1的任何原因。插入时注意是否有多余的空间,插入前可以使用修剪。大小写也很重要,“Claude”与“Claude”不同。从代码的外观来看,您需要遍历ArrayList,逐个元素执行字符串#包含在每个元素上好。。而罗希特·贾因·克劳德则是更大的一部分。
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.
    }
}
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);
  }
}