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,
*/
}