2013-10-24 80 views
0

我目前使用JSoup從HTML表中抓取數據,然後將其存儲到ArrayList中。然後我想將這些值存儲在SQL數據庫中。從我的理解,這些必須轉換爲一個字符串,然後才能被插入到數據庫中。我將如何去將這些ArrayLists轉換爲一個字符串?將ArrayList轉換爲字符串

這裏是我當前的代碼:

Document doc = Jsoup.connect(URL).timeout(5000).get(); 

    for (Element table : doc.select("table:first-of-type")) 
    { 
     for (Element row : table.select("tr:gt(0)")) { 
      Elements tds = row.select("td"); 

      List1.add(tds.get(0).text()); 
      List2.add(tds.get(1).text()); 
      List3.add(tds.get(2).text()); 
     } 
    } 

回答

2

得到你需要,你可以遍歷列表很容易的所有值:

public static void main(String[] args) { 
    List<String> List1 = new ArrayList<>(); 
    for (String singleString : List1){ 
     //Do whatever you want with each string stored in this list 
    } 
} 
0

Java集合框架並沒有提供任何直接的在Java中將ArrayList轉換爲String的實用方法。但是以依賴注入和它的IOC容器而聞名的Spring框架還提供了API,它提供了一些常用的工具,例如將Collection轉換爲Java中的String的方法。您可以使用Spring框架的StringUtils類將ArrayList轉換爲String。 StringUtils類提供三種方法來轉換任何集合

ArrayList中爲String在Java中,如下圖所示:

public static String collectionToCommaDelimitedString(Collection coll) 
public static String collectionToDelimitedString(Collection coll, String delim) 
public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) 

得到它....

0

嗯,我真的不知道,如果有,是實現你所要求的一些特殊的方法。 您需要遍歷數組。您可以使用StringBuilder而不是String來進行一些優化。我真的不知道多少你會提振的漲幅表現..

反正這是代碼是什麼樣子..

StringBuilder sb = new StringBuilder(); 
for (Object o : list1){ 
    sb.append(o.toString()+delimiter); // delimiter could be , or \t or || 

//You could manipulate the string here as required.. 
// enter code here` 
} 
return sb.toString(); 

你會那麼需要的,如果需要分割的字符串。 (如果他們需要進入單獨的領域