using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*二维数组行列互换显示出来*/
namespace 二维数组
{
internal class Program
{
static void Main(string[] args)
{
int[,] arr2 = new int[3, 3] { { 1,2,3 }, { 4,5,6 }, { 7,8,9 } };
int m = arr2.GetLength(0);
int n = arr2.GetLength(1);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(arr2[i, j] + " ");
}
Console.WriteLine();
}
for (int i = 0; i < m; i++) {
for(int j = i; j < n; j++) {
//这里要注意j从i处出发,否则就会互换两次变回去了
int geti = arr2[i, j];
arr2[i, j] = arr2[j, i];
arr2[j, i] = geti;
} }
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
Console.Write(arr2[i, j]+" ");
}
Console.WriteLine();
}
}
}
}
二维数组行列互换显示出来
最新推荐文章于 2024-11-11 19:01:07 发布