在C#中找出两个二维数组之间的差异可以通过以下步骤完成:
以下是一个示例代码,展示如何在C#中找出两个二维数组之间的差异:
int[,] arrayA = new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
int[,] arrayB = new int[,]
{
{ 1, 2, 3 },
{ 4, 0, 6 },
{ 7, 8, 9 }
};
List<Tuple<int, int>> differences = new List<Tuple<int, int>>();
int rows = arrayA.GetLength(0);
int columns = arrayA.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if (arrayA[i, j] != arrayB[i, j])
{
differences.Add(new Tuple<int, int>(i, j));
}
}
}
if (differences.Count > 0)
{
Console.WriteLine("Found differences between arrayA and arrayB at the following positions:");
foreach (var difference in differences)
{
Console.WriteLine($"Row: {difference.Item1}, Column: {difference.Item2}");
}
}
else
{
Console.WriteLine("No differences found between arrayA and arrayB.");
}
这段代码将输出数组A和数组B之间的差异位置,对于示例中的数组A和数组B,输出结果为:
Found differences between arrayA and arrayB at the following positions:
Row: 1, Column: 1
领取专属 10元无门槛券
手把手带您无忧上云