C#多维数组学习使用

时间:2021-05-20

复制代码 代码如下:
String[, ,] items =
new String[,,] {
{
{ "A1", "A2", "A3", "☆", "○" },
{ "B1", "B2", "B3", "☆", "○" },
{ "C1", "C2", "C3", "☆", "○" },
{ "D1", "D2", "D3", "☆", "○" }
}, {
{ "E1", "E2", "E3", "☆", "○" },
{ "F1", "F2", "F3", "☆", "○" },
{ "G1", "G2", "G3", "☆", "○" },
{ "H1", "H2", "H3", "☆", "○" }
}
};

代码:
复制代码 代码如下:
System.Console.WriteLine("Items.Rank =" + items.Rank);
System.Console.WriteLine("Items.GetUpperBound(0)=" + items.GetUpperBound(0));
System.Console.WriteLine("Items.GetUpperBound(1)=" + items.GetUpperBound(1));
System.Console.WriteLine("Items.GetUpperBound(2)=" + thirdItems.GetUpperBound(items.Rank - 1));

System.Console.WriteLine("Items[0, 0, 0]=" + items[0, 0, 0]);
System.Console.WriteLine("Items[0, 0, 1]=" + items[0, 0, 1]);
System.Console.WriteLine("Items[0, 0, 2]=" + items[0, 0, 2]);
System.Console.WriteLine("Items[0, 0, 3]=" + items[0, 0, 3]);
System.Console.WriteLine("Items[0, 0, 4]=" + items[0, 0, 4]);

System.Console.WriteLine("Items[0, 1, 0]=" + items[0, 1, 0]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 1]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 2]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 3]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 4]);

运行结果如下:
复制代码 代码如下:
Items.Rank =3
Items.GetUpperBound(0)=1
Items.GetUpperBound(1)=3
Items.GetUpperBound(2)=4
Items[0, 0, 0]=A1
Items[0, 0, 1]=A2
Items[0, 0, 2]=A3
Items[0, 0, 3]=☆
Items[0, 0, 4]=○
Items[0, 1, 0]=B1
Items[0, 2, 0]=B2
Items[0, 2, 0]=B3
Items[0, 2, 0]=☆
Items[0, 2, 0]=○

其中:
GetUpperBound(0) 返回数组的第一维的索引上限,GetUpperBound(i)返回数组的i+1维的上限,GetUpperBound(Rank-1)返回数组的最后一维的上限,也就是列数-1

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章