用独立的元素和其他列表中的元素填充java列表的最佳方式是什么



我需要创建一个链表,并用容器中的"容器"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());

相关内容

  • 没有找到相关文章