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#代码.
展开
4个回答
展开全部
有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>(); //保留重复项
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>(); //保留重复项
展开全部
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]);
}
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]);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我假设你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]);
}
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]);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var t = list1.Select(p => p + list3[list1.FindIndex(item => item.Equals(p))]).ToList();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询