相互添加两个数组的元素 [英] Add elements of two array with each other

查看:92
本文介绍了相互添加两个数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Int类型的数组

I have 2 Array of type Int like this

let arrayFirst = [1,2,7,9]
let arraySecond = [4,5,17,20]

我想添加元素每个数组,如arrayFirst [0] + arraySecond [0],arrayFirst [1] + arraySecond [1]等等,并将其分配给另一个数组,因此数组的结果将类似于

I want to add the elements of each array, like arrayFirst[0] + arraySecond[0], arrayFirst[1] + arraySecond[1] an so on and assign it to another array, so the result of the array would be like


[5,7,24,29]

[5, 7, 24, 29]

什么是使用 swift3实现此目的的最佳实践

推荐答案

您可以添加两者像这样的数组

You can add both the arrays like this

let arrayFirst = [1,2,7,9]
let arraySecond = [4,5,17,20]

let result = zip(arrayFirst, arraySecond).map(+)
print(result)

这篇关于相互添加两个数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆