簡體   English   中英

如何在JAVA中使用三個foreach循環創建單個3元素集合列表?

[英]How to create a single 3 element collection list using three foreach loop in JAVA.?

我想創建一個包含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) 

我應該如何從每個循環中添加每個元素,以便我應該將pairlist作為

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

如果三個列表的大小相同,則可以嘗試類似

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()
                );
    ...
}

空案例處理被忽略。

普通的for循環怎么樣?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM