[英]What best way to populate java list with separate elements and elements from other lists?
我需要创建一个链表,并用容器中的“容器”和“元素”填充它。
这是我的代码:
public List<WebElement> getExpectedElements(){
List<WebElement> list = new LinkedList<WebElement>(Arrays.asList(
inetConnection,
wiredConnection,
phonesConnection,
usbConnection,
wifiConnection
));
list.addAll(inetConnection.getExpectedElements());
list.addAll(wiredConnection.getExpectedElements());
list.addAll(phonesConnection.getExpectedElements());
list.addAll(usbConnection.getExpectedElements());
list.addAll(wifiConnection.getExpectedElements());
return list;
}
在java中是否有任何方法可以使它更好(更简洁,干燥等)?
你至少可以引入一个循环:
List<WebElement> containers = Arrays.asList(inetConnection,
wiredConnection,
phonesConnection,
usbConnection,
wifiConnection);
List<WebElement> list = new LinkedList<WebElement>(containers);
for (WebElement e : containers)
list.addAll(e.getExpectedElements());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.