C# List全包含另一个List
【摘要】 C# List全包含另一个List
class Program { static void Main(string[] args) { #region 全部包含 List<int> list1 = new List<int>(); list1.Add(1); list1.Add(2); list1.Add(2); list1.Add(2);...
C# List全包含另一个List
class Program { static void Main(string[] args) { #region 全部包含 List<int> list1 = new List<int>(); list1.Add(1); list1.Add(2); list1.Add(2); list1.Add(2); List<int> list2 = new List<int>(); list2.Add(3); list2.Add(2); list2.Add(2); list2.Add(1); Console.Write("list1: "); for (int i = 0; i < list1.Count; i++) { Console.Write(list1[i] + " "); } Console.WriteLine(); Console.Write("list2: "); for (int i = 0; i < list2.Count; i++) { Console.Write(list2[i] + " "); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("list1包含list2: " + list2.All(t => list1.Any(b => b == t))); Console.WriteLine("list2包含list1: " + list1.All(t => list2.Any(b => b == t))); Console.WriteLine("list1包含list2: " + ContainsAll(list1, list2)); Console.WriteLine("list2包含list1: " + ContainsAll(list2, list1)); Console.WriteLine("list1真包含list2: " + ContainsAll_Really(list1, list2)); Console.WriteLine("list2真包含list1: " + ContainsAll_Really(list2, list1)); Console.WriteLine(); //全部移除 list2.RemoveAll(li => list1.Contains(li)); Console.Write("list1: "); for (int i = 0; i < list1.Count; i++) { Console.Write(list1[i] + " "); } Console.WriteLine(); Console.Write("list2: "); for (int i = 0; i < list2.Count; i++) { Console.Write(list2[i] + " "); } #endregion; Console.ReadKey(); } /// <summary> /// List_A 全包含 List_B /// [1,2,3] --> [1,2,2] = true /// [1,2,2,3] --> [1,2,2] = true /// </summary> /// <param name="list_A">列表A</param> /// <param name="list_B">列表B</param> static bool ContainsAll(List<int> list_A, List<int> list_B) { for (int i = 0; i < list_B.Count; i++) { if (!list_A.Contains(list_B[i])) return false; } return true; } /// <summary> /// List_A 真全包含 List_B /// [1,2,3] --> [1,2,2] = false /// [1,2,2,3] --> [1,2,2] = true /// </summary> /// <param name="list_A">列表A</param> /// <param name="list_B">列表B</param> /// <returns></returns> static bool ContainsAll_Really(List<int> list_A, List<int> list_B) { List<int> list_1 = new List<int>(list_A); for (int i = 0; i < list_B.Count; i++) { if (!list_1.Contains(list_B[i])) return false; else list_1.Remove(list_B[i]); } return true; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/103923935
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)