gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:52:14 26 4
gpt4 key购买 nike

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

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

关于java - 将所有最后的元素拆分并存储到一个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34416932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com