Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何在Groovy中实现列表的元素添加?_List_Groovy - Fatal编程技术网

List 如何在Groovy中实现列表的元素添加?

List 如何在Groovy中实现列表的元素添加?,list,groovy,List,Groovy,是否可以通过Groovy闭包或内置功能为list添加元素?使用加法运算符会导致简单的列表串联 def firstList = [1, 2, 3, 4] def secondList = [5, 6, 7, 8] def resultList = firstList + secondList // It has to be by elements addition closure here assert resultList == [6, 8, 10, 12] // Assertion fail

是否可以通过Groovy闭包或内置功能为list添加元素?使用加法运算符会导致简单的列表串联

def firstList = [1, 2, 3, 4]
def secondList = [5, 6, 7, 8]
def resultList = firstList + secondList // It has to be by elements addition closure here

assert resultList == [6, 8, 10, 12] // Assertion failed

只要小心,如果两个列表的长度不同,则结果列表的长度将是这两个列表中的最小值。正确。最好的情况是两个列表大小相同@Ghoshak请注意,如果两个列表的长度不同,则结果列表的长度将是这两个列表中的最小值。正确。最好的情况是两个列表大小相同@戈沙克
[ firstList, secondList ].transpose()*.sum()