c# – 以矩阵格式打印2D数组
发布时间:2020-12-15 04:36:09 所属栏目:百科 来源:网络整理
导读:我有一个2D数组如下: long[,] arr = new long[4,4] {{ 0,0 },{ 1,1,1 },{ 0,1 }}; 我想以矩阵格式打印这个数组的值,如: 0 0 0 01 1 1 10 0 0 01 1 1 1 我该怎么做? 解决方法 您可以这样做(使用稍微修改的数组来显示它适用于非正方形数组): long[,] arr =
我有一个2D数组如下:
long[,] arr = new long[4,4] {{ 0,0 },{ 1,1,1 },{ 0,1 }}; 我想以矩阵格式打印这个数组的值,如: 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 我该怎么做? 解决方法
您可以这样做(使用稍微修改的数组来显示它适用于非正方形数组):
long[,] arr = new long[5,4] { { 1,2,3,4 },{ 2,2 },{ 3,3 },{ 4,4,4 } }; int rowLength = arr.GetLength(0); int colLength = arr.GetLength(1); for (int i = 0; i < rowLength; i++) { for (int j = 0; j < colLength; j++) { Console.Write(string.Format("{0} ",arr[i,j])); } Console.Write(Environment.NewLine + Environment.NewLine); } Console.ReadLine(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |