2013-02-20 75 views
7

वेबसाइट से प्राप्त करने के लिए मुझे जो कुछ चाहिए, उसके साथ ArrayList को पॉप्युलेट करने के लिए वेबसाइट से Jsoup I parse HTML का उपयोग करके। तो अब मेरे पास ArrayList है जो स्ट्रिंग से भरा हुआ है। मैं उस सूची में इंडेक्स ढूंढना चाहता हूं जिसमें एक निश्चित स्ट्रिंग है। उदाहरण के लिए, मुझे पता है कि सूची में कहीं भी, कुछ इंडेक्स में स्ट्रिंग (शाब्दिक) "क्लाउड" है, लेकिन मुझे लगता है कि में contains "क्लाउड" इंडेक्स को कोई कोड नहीं मिल रहा है ... यहाँ मैं क्या कोशिश की है है, लेकिन -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); 
     } 
    } 
} 
+3

है 'Claude' बड़ा स्ट्रिंग, या अपने आप पर सूची में एक तार का एक हिस्सा है? –

+0

स्ट्रिंग 'ए' मुद्रित करने का प्रयास करें और" क्लाउड "की जांच करें। यह वहां नहीं होना चाहिए। JSoup – LGAP

+0

का उपयोग करके एचटीएमएल टैग को पुन: सक्रिय करने के तरीके पर कार्य करें, यदि सूची में "क्लाउड" जोड़ा गया है, तो मुझे -1 प्राप्त करने का कोई कारण नहीं दिखता है। डालने के दौरान अतिरिक्त रिक्त स्थान के लिए देखो, डालने से पहले ट्रिम का उपयोग कर सकते हैं। मामला भी मायने रखता है, "क्लाउड" "क्लाउड" से अलग है। – sudmong

उत्तर

25

आप 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) { 
    // 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); 
    } 
} 
0
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. 
    } 
}