我需要创建一个链表,并用容器中的"容器"one_answers"元素"填充它。
这是我的代码:
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中有没有什么方法可以让它变得更好(更简洁、更DRY等)?
您至少可以引入一个循环:
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());