Java 将最后的所有元素拆分并存储到一个数组中
我想将检索到的字符串值存储到一个数组或列表中,然后对它们进行排序。我尝试了许多方法以上述方式存储它们。但没有任何帮助。有人能在这方面帮助我吗Java 将最后的所有元素拆分并存储到一个数组中,java,arrays,list,selenium,selenium-webdriver,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]; fo
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]
}
List List=webDriver.findElements(By.xpath(expression));
int listcount=list.size();
列表选项列表=空;
字符串选项[]=新字符串[listcount];
对于(inti=1;i,这里是用于获取数组中所有姓氏的修改代码
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.
List List=webDriver.findElements(By.xpath(expression));
int listcount=list.size();
列表选项列表=空;
字符串选项[]=新字符串[listcount];
对于(int i=1;i您可以修改代码,如下所示:
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);
for(int i=1;i您面临的问题是什么?我尝试了以下代码。但它总是只存储上次检索选项的姓氏。List strArray=new ArrayList();for(int k=0;koptions[i-1]=webDriver.findElement(By.xpath(“表达式”).getText();
尝试options[i-1]=list.get(i-1.getText();
选项[i-1]没有问题。相反,无法将检索到的姓氏存储到列表中。是否调试代码并检查数组expectedOptions
是否正确填充?是的,Harbeer也提供了相同的解决方案。感谢您的帮助。