我想要创建一个单列表,其中包含3个元素,每个元素从3个不同的foreach循环中获得值。
for (WebElement webElement : li) {
key = webElement.getText().trim().substring(10, 16) ;
}
//e.g {1,2,3,4,5,.......1000}
for (String string : defectidList) {
defectid =string;
}
//e.g {abc-1,abc-2,abc-3,abc-4,abc-5,......1000}
for (WebElement element : hreflist) {
hreflink =element.getText();
}
//e.g. {abc.com,bcd.com,def.com,abc.com,bcd.com,....1000}
Class Pair{
String key;
String defectid;
String hreflinks;
Pair(String k,String d, String h){
this.key=k;
this.defectid=d;
this.hreflinks=h;
}
}
ArrayList<Pair> pairlist = new ArrayList<Pair>
Pair p = new Pair(key,defectid,hreflinks)
pairlist.add(p) 如何从每个循环中添加每个元素,以便以的形式获得配对列表
1 abc-1 abc.com
2 abc-2 bcd.com
3 abc-3 def.com
4 abc-4 abc.com
5 abc-5 bcd.com发布于 2014-08-15 13:31:36
如果三个列表的大小相同,您可以尝试这样的方法
for(int i=0;i<li.size();i++){
...
Pair p = new Pair(
li.get(i).getText().trim().substring(10, 16),
defectidList.get(i),
hreflink.get(i).getText()
);
...
}空案例处理被忽略。
发布于 2014-08-15 13:37:00
正常的循环怎么样?
ArrayList<Pair> = new ArrayList<Pair>;
for(int i = 0; i < li.length && i < defectidList.lengh && i < hrefList.length; i++){
String key = li[i].getText().trim().substring(10, 16);
String hrefLink = hrefList[i].getText();
pairList.add(new Pair(key, defectidList[i], hrefLink));
}https://stackoverflow.com/questions/25327175
复制相似问题