令人困惑的索引越界
我的代码中有一部分需要反转矩阵。这只能在方阵上合理地完成,在本例中是 3x3 方阵。我用来反转矩阵的工具一直说我的数组不是一个正确的正方形。
所以我做了一个小测试:
double[,] x = new double[3, 3];
MessageBox.Show(x.GetLength(0).ToString());
MessageBox.Show(x.GetLength(1).ToString());
MessageBox.Show(x.GetLength(2).ToString());
第一个显示为“3”。第二个出现为“3”。第三个出现为 IndexOutOfRangeException
。我只是忽略了一些非常明显的事情还是......这有点奇怪?
(注意:这是使用 .Net 2.0 的 C# 代码)
There a section in my code where I need to invert a matrix. That can only reasonably be done on a square matrix, in this case a 3x3 square matrix. The tool I'm using to invert the matrix kept saying that my array wasn't a proper square.
So I did a little test:
double[,] x = new double[3, 3];
MessageBox.Show(x.GetLength(0).ToString());
MessageBox.Show(x.GetLength(1).ToString());
MessageBox.Show(x.GetLength(2).ToString());
First one comes up as "3". Second one comes up as "3". Third one comes up as an IndexOutOfRangeException
. Am I just overlooking something extremely obvious or... is this a little weird?
(Note: This is code from C# using .Net 2.0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您只有一个二维数组。为什么您希望询问第三个维度的大小才能得到有效的结果?
Array.GetLength()
方法返回数组指定维度中的元素数量。在您的情况下:您询问一个二维数组,它的第三维的大小是多少。结果是
IndexOutOfRangeException
。这是预期的行为。在您的代码示例中,您似乎可能会混淆每个指定维度的大小与维度数。以下是不同维度的矩形数组的一些示例:
看到模式了吗?维数由您在数组声明中指定的数字数量决定。每个尺寸的大小由声明中每个数字的值确定。
You only have an array with two dimensions. Why would you expect asking for the size of the third dimensions to give you a valid result?
The
Array.GetLength()
method return the number of elements in the specified dimension of the Array. In your case:you're asking a 2-dimensional array what the size of it's third dimension is. The result is an
IndexOutOfRangeException
. This is the expected behavior.In your code example, it looks like you may be confusing the size of each stated dimension, with the number of dimensions. Here are some examples of rectangular arrays of different dimensions:
See the pattern? The number of dimensions is determined by how many numbers you specify in the array declaration. The sizes of each dimension is determined by the values of each number in the declaration.
你的矩阵只有 2 维,长度均为 3 个元素。第三个
MessageBox
行尝试打印不存在的第三个维度的长度。Your matrix only has 2 dimensions, both having length of 3 elements. The third
MessageBox
line is trying to print the length of a non-existent third dimension.只是为了提供一个直观的示例并用我自己的话提供答案...(我一开始也无法理解为什么抛出异常):
double[,] x = new double[3, 3] ;
给出一个看起来像这样的数组:我认为代码试图找到可能被认为是子数组的长度,其中每一行代表一个数组,而该数组又是一个更大数组的成员。
实际上,行本身并不是数组,只是原始数组的成员(双精度)。
GetLength
方法正在查找数组的维度而不是长度(这可能会令人困惑,因为Length
用于确定数组中成员的数量)。如果您愿意的话,该数组有两个维度:X
维度和Y
维度。将其想象为一张具有两个维度的平板。如果添加第三个:
工作表变成立方体,添加新维度
Z
,并且第三个GetLength
将起作用。Just to provide a visual example and provide an answer in my own words... (I failed at first to see why the exception was being thrown, also):
double[,] x = new double[3, 3];
gives an array that looks like this:I thought that the code was trying to find the length of what might be thought of as a subarray, where each row represents an array that is in turn a member of a larger array.
In actuality the rows are not themselves arrays, just members (doubles) of the original array. The
GetLength
method is looking for the dimensions of the array rather than the length (which might be confusing becauseLength
is used to determine the number of members in an array). This array has two dimensions, theX
dimension and theY
dimension, if you will.Think of it like a flat sheet, which has two dimensions. If you add a third:
The sheet becomes a cube, a new dimension
Z
is added, and the thirdGetLength
would work.您的数组中没有定义 3 个维度,因此只能获得 2 个维度的长度。您的代码要求的是立方体的尺寸,而不是正方形的尺寸。
You don't have 3 dimensions defined in your array, so you can only get the length of two. Your code is asking for the dimensions of a cube, not a square.
这可以帮助您形象化所解释的内容。
您声明的 x 数组如下所示:
This may help you visualize what's been explained.
Your array of x as you've declared it looks like this: