将一个二维数组行和列元素互换,存到另一个二维数组中。
public static void CTL(int[,] arr1)
{
int[,] arr2 = new int[3,3];
for (int i = 0; i < 3; i++)
{
for (int j= 0; j < 3; j++)
{
arr2[i, j] = arr1[j,i];
}
}
Console.WriteLine("互换后:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr2[i, j]+" ");
}
Console.WriteLine("\n");
}
}
static void Main(string[] args)
{
int[,] arr = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Console.WriteLine("互换前:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine("\n");
}
CTL(arr);
Console.ReadLine();
}