首页 / C# / 使用c#访问2d数组时出错
使用c#访问2d数组时出错
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用c#访问2d数组时出错,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1647字,纯文字阅读大概需要3分钟。
内容图文
我是Unity3D和c#的新手.我正在修补在2d阵列中存储一些网格位置但是我遇到了
数组索引超出范围
错误,我不知道为什么:
public int[,] myArray;
myArray = new int[,]{
{0,375},
{75,300},
{150,225},
{225,150},
{300,75},
{375,0}
};
Debug.Log(myArray[1,4]); // array index is out of range... why? I expected to get 75.
以下是我正在寻求帮助的其他一些资源:
http://wiki.unity3d.com/index.php/Choosing_the_right_collection_type
https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
解决方法:
你有一个6×2的2D数组 – 而不是2×6的2D数组.如果您考虑通过数组[row,column]访问数组,那么您在初始化中指定的每个“子数组”都是一个“行”.
因此,例如,myArray [0,1]是375 – 第一个“行”的第二个元素,即{0,375}.
基本上,您需要转动阵列初始化或阵列访问.所以如果你真的想要一个2×6阵列,你需要:
myArray = new int[,] {
{ 0, 75, 150, 225, 300, 375 },
{ 375, 300, 225, 150, 75, 0 }
};
…或者您可以保留现有的初始化,并访问myArray [4,1].
C#规范解释如下:
For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level. The example:
06001
creates a two-dimensional array with a length of five for the leftmost dimension and a length of two for the rightmost dimension:
06002
and then initializes the array instance with the following values:
06003
内容总结
以上是互联网集市为您收集整理的使用c#访问2d数组时出错全部内容,希望文章能够帮你解决使用c#访问2d数组时出错所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。