2013-10-22 24 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