C# 如何把两个list的元素相加

例如:list1里有1,2,3,4,5.list2里有6,7,8,9,10.最后要得到一个list3为1+6,2+7,3+8,4+9,5+10.也就是list1和list... 例如:list1里有1,2,3,4,5.list2里有6,7,8,9,10.最后要得到一个list3为1+6,2+7,3+8,4+9,5+10.也就是list1和list2的元素对应相加.请给一个完整的C#代码. 展开
 我来答
微凉如烟轻若梦
2019-06-13 · TA获得超过1334个赞
知道小有建树答主
回答量:257
采纳率:60%
帮助的人:70.8万
展开全部
有Union和Concat两个函数

List<int> listA = new List<int> { 1, 4, 8, 9, 7, 8, 3 };

List<int> listB = new List<int> { 13, 4, 17, 29, 2 };

List<int> ResultA = listA.Union(listB).ToList<int>(); //剔除重复项

List<int> ResultB = listA.Concat(listB).ToList<int>(); //保留重复项
百度网友94f02eb
推荐于2016-09-16 · TA获得超过8612个赞
知道大有可为答主
回答量:7955
采纳率:74%
帮助的人:4377万
展开全部
List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
List<int> list2 = new List<int> { 6, 7, 8, 9, 10 };
List<int> list3 = new List<int>();

for (int i = 0; i < list1.Count; i++)
{
list3.Add(list1[i] + list2[i]);
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
葉大師八一d2
2009-12-26 · 超过12用户采纳过TA的回答
知道答主
回答量:40
采纳率:37%
帮助的人:21.2万
展开全部
我假设你List中存放的是int型
List<int> list3 = new List<int>();
int length = Math.Min(list1.Count, list2.Count);
for (int i = 0; i < length; i++)
{
list3.Add(list1[i] + list2[j]);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友f1aa012
2019-09-21
知道答主
回答量:16
采纳率:100%
帮助的人:5.2万
展开全部
var t = list1.Select(p => p + list3[list1.FindIndex(item => item.Equals(p))]).ToList();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式