0

我需要创建一个链表并用容器中的“容器”和“元素”填充它。

这是我的代码:

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等)?

4

1 回答 1

2

你至少可以引入一个循环:

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());
于 2013-08-07T19:28:43.680 回答