将所有最后的元素拆分并存储到数组中

时间:2015-12-22 13:24:53

标签: java arrays list selenium selenium-webdriver

我想将检索到的字符串值存储到数组或列表中然后对它们进行排序。我已经尝试了很多方法以上述方式存储它们。但没有什么是有用的。在这方面有人可以帮助我。

List<WebElement> list = webDriver.findElements(By.xpath(expression));
int listcount = list.size();    
List optionsList = null;
String options[] = new String[listcount];

for (int i=1; i<=listcount; i++) {                     
    options[i-1] = webDriver.findElement(By.xpath("expression")).getText();
    //contains three words eg: Test Engineer Madhu               
    String[] expectedOptions = options[i-1].split(" ");
    //splitting based on the space           
    String lastName =expectedOptions[expectedOptions.length-1]; 
    //taking the lastName.
    List<String> strArray = new ArrayList<String>();
    for (int k = 0; k < i; k++) {
       strArray.add(lastName);        
    }
    optionsList = Arrays.asList(lastName);
}          
System.out.println(optionsList);
//This always results in last option's last name eg: [Madhu]
}      

现在,我希望将所有姓氏存储在一个数组中,以验证它们是否按排序顺序排列。

2 个答案:

答案 0 :(得分:1)

以下是修改代码以获取数组中的所有姓氏

List<WebElement> list = webDriver.findElements(By.xpath(expression));
int listcount = list.size();    
List optionsList = null;
String options[] = new String[listcount];

for (int i=1; i<=listcount; i++) 
{                     
    options[i-1] = webDriver.findElement(By.xpath("expression")).getText();
    //contains three words eg: Test Engineer Madhu               
    String[] expectedOptions = options[i-1].split(" ");
    //splitting based on the space           
    // I hope the next line gives you the correct lastName
    String lastName =expectedOptions[expectedOptions.length-1]; 
    // lets store the last name in the array
    options[i-1] = lastName;        
 }         
 optionsList = Arrays.asList(options);
 // Iterate through the list to see all last names.

答案 1 :(得分:0)

您可以修改代码,如:

for (int i=1; i<=listcount; i++) {  
        //contains three words eg: Test Engineer Madhu               
        String[] expectedOptions = options[i-1].split(" ");
        //splitting based on the space           
        String lastName =expectedOptions[expectedOptions.length-1]; 
        //taking the lastName.
        options[i - 1] = lastName;
    }
    optionsList = Arrays.asList(options); 
    System.out.println(optionsList);

希望它有所帮助。