C#程序打印两个列表的所有常见元素
首先创建两个列表-
List list1 = new List() {40, 20, 60, 3, 55};
List list2 = new List() {20, 70, 55, 80};要找到常见元素,请使用相交-
list1.Intersect(list2)
以下是查找两个列表之间的公共元素的完整代码-
示例
using System;
using System.Linq;
using System.Collections.Generic;
namespace Demo {
class Program {
static void Main(string[] args) {
List list1 = new List() {40, 20, 60, 3, 55};
List list2 = new List() {20, 70, 55, 80};
Console.WriteLine("常用元素:");
foreach(int value in list1.Intersect(list2))
Console.WriteLine(value);
}
}
}输出结果
常用元素: 20 55
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短