c# - C#访问与多维数组混合的锯齿状数组中的行
问题描述
我想ith
通过使用简单地访问该行street[i]
。
int[][,] street = new int[7][,] {
new int [,] { {0, 0}, {0, 0}},
new int [,] { {0, 1 }, {0, -1} },
new int [,] { {-1, 0 }, {1, 0} },
new int [,] { {1, 0 }, {0, -1} },
new int [,] { {1, 0 }, {0, 1} },
new int [,] { {-1, 0 }, {0, -1} },
new int [,] { {-1, 0 }, {0, 1} },
};
System.Console.WriteLine(street[0]); # print out System.Int32[,]
我本以为会得到[[0,0], [0,0]]
,但扔掉了System.Int32[,]
。为什么?
解决方案
您可以序列化对象并打印结果。
我将使用Newtonsoft.Json你可以使用任何你喜欢的序列化器。
Install-Package Newtonsoft.Json
创建数组后,只需序列化对象并打印结果。
using Newtonsoft.Json;
int[][,] street = new int[7][,] {
new int [,] { {0, 0}, {0, 0}},
new int [,] { {0, 1 }, {0, -1} },
new int [,] { {-1, 0 }, {1, 0} },
new int [,] { {1, 0 }, {0, -1} },
new int [,] { {1, 0 }, {0, 1} },
new int [,] { {-1, 0 }, {0, -1} },
new int [,] { {-1, 0 }, {0, 1} },
};
Console.WriteLine(JsonConvert.SerializeObject(street[0]));
//Or you can print all the array
//Console.WriteLine(JsonConvert.SerializeObject(street));
推荐阅读
- loops - 关于自制数字猜谜游戏的问题
- unit-testing - Flutter 测试:InputChip 点击删除
- javascript - 我需要 JavaScript/jQuery 吗?
- entity-framework - 在 aspnetzero 框架中没有从 linq 查询中获取值
- node.js - Firebase Firestore 事务非常慢(3-4 分钟)
- django - Django PermissionRequiredMixin 限制对特定用户的访问
- javascript - 如何用打字稿中的单个单词替换多个单词?
- redis - Redis - 排序集 ZRANGE 性能
- javascript - javascript if 语句在错误条件下触发
- javascript - audio.oncanplaythrough 在 Internet explore 上不起作用