2015-12-22 2 views
2

Я хотел сохранить значения строк, извлеченные в массив или список, а затем отсортировать их. Я попробовал много способов сохранить их указанным образом. Но ничего не полезно. может кто-то, пожалуйста, помогите мне в этом отношении.Сплит и сохраните все последние элементы в массиве

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] 
}  

Теперь я хочу, чтобы все последние имена, которые будут храниться в массиве, чтобы проверить, если они находятся в отсортированном порядке или нет.

+0

В чем проблема, с которой вы столкнулись? – Paras

+0

Я пробовал приведенный ниже код. Но он всегда хранит только последнюю фамилию последнего найденного параметра. Список strArray = новый ArrayList (); \t для (int k = 0; k Rahul

+0

Вместо 'options [i-1] = webDriver.findElement (By.xpath (" expression ")). GetText();' try 'options [i-1] = list.get (i- 1) .getText(); ' – Paras

ответ

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. 
+0

Спасибо большое:) Это сработало для меня. – Rahul

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); 

Надеется, что это помогает.

+0

Да, Harbeer также предоставил такое же решение. Спасибо за вашу помощь. – Rahul

Смежные вопросы