2013-10-22 38 views
0

我有一個鏈接列表,它們正在增加一個我想採用該字符串類型的xpath並將其拆分到數字所在的位置,並且每次將該數字更改一次循環。如何在java中循環遍歷一個已解析的字符串

String vPath= "/html/body/section/section[2]/a[1]"; 
//so the number that's changing is in the last bracket 
String[] t = vPath.split("/a"); 

然後我想使用分割變量並循環遍歷。所以也許使用for循環。不過,我似乎有一個問題,我該怎麼做。我認爲它應該像

For (int i=1; i < t(something here); i++{ 
then the code of clicking should go here 
} 

請幫忙。

回答

0

如果有最近JDK工作(1.5+),然後使用:

for (String chunk: vPath.split("/a")) { 
    System.out.println(chunk); 
    // and anything else with chunk here... 
} 

但在此之前,請獲得良好的Java書籍和學習數組和集合。

+0

是你正確的讀一本書將幫助。謝謝! – Madi

1

按我你的問題的理解,解決方案如下:

  String vPath= "/html/body/section/section[2]/a[1]"; //sample url 
     int size = 100; // no of links you want to generate as per your requirement 
     String[] chunks = vPath.split("/a"); 
     String chunk = chunks[0]; 
     for(int index =1; index <= size;index++){ 
      System.out.println(chunk+"["+index+"]"); // printing generated urls 
      //code of clicking... 
     } 
+0

偉大的工作!我很感激!!!! – Madi