- 打卡等级:常驻代表
- 打卡总天数:34
- 打卡月天数:6
- 打卡总奖励:9027
- 最近打卡:2025-12-17 23:15:51
管理员
- 积分
- 22569
|
|
C# 通过循环的方式遍历数组中不相同的元素
- static void Main(string[] args)
- {
- int[] arrA = { 1, 2, 3, 4, 5, 9 };
- int[] arrB = { 1, 4, 5, 7, 8, 9 };
-
- string result = "";
- // arrA中的元素不再arrB中的结果
- foreach (int item in arrA)
- {
- if (Array.IndexOf(arrB, item) <0)
- {
- result += item.ToString() + ",";
- }
- }
-
- // arrB中的元素不再arrA中的结果
- foreach (int item in arrB)
- {
- if (Array.IndexOf(arrA, item) < 0)
- {
- result += item.ToString() + ",";
- }
- }
-
- Console.Write("不相同的元素:");
- Console.WriteLine(result);
- Console.Read();
- /*
- ------输出结果------------
- 不相同的元素:2,3,7,8,
- */
- }
复制代码
来源:C#社区
|
|