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

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

问题描述

我有 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天全站免登陆