class Program
{
public static string[,] Rotate(string[,] array)
{
int x = array.GetUpperBound(0); //一维
int y = array.GetUpperBound(1); //二维
string[,] newArray = new string[y 1, x 1]; //构造转置二维数组
for (int i = 0; i <= x; i )
{
for (int j = 0; j <= y; j )
{
newArray[j, i] = array[i, j];
}
}
return newArray;
}
static void Main(string[] args)
{
string[,] array = new string[4, 2];
for (int i = 0; i < 4; i )
{
for (int j = 0; j < 2; j )
{
array[i, j] = i.ToString() j.ToString();
}
}
//显示原数组
Console.WriteLine("Source Array:");
for (int i = 0; i < 4; i )
{
string soureResult = string.Empty;
for (int j = 0; j < 2; j )
{
soureResult = array[i, j] " ";
}
Console.WriteLine(soureResult);
}
string[,] newArray = Rotate(array);
//显示转置后的数组
Console.WriteLine("Destiney Array:");
for (int i = 0; i < 2; i )
{
string dstResult = string.Empty;
for (int j = 0; j < 4; j )
{
dstResult = newArray[i, j] " ";
}
Console.WriteLine(dstResult);
}
Console.ReadLine();
}
}
好了,下面接着看二维数组列表List<>的转置