令人困惑的索引越界

发布于 2024-09-15 06:50:18 字数 427 浏览 2 评论 0原文

我的代码中有一部分需要反转矩阵。这只能在方阵上合理地完成,在本例中是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

许你一世情深 2024-09-22 06:50:18

您只有一个二维数组。为什么您希望询问第三个维度的大小才能得到有效的结果?

Array.GetLength() 方法返回数组指定维度中的元素数量。在您的情况下:

x.GetLength(2).ToString();   // asking for size of third dimension

您询问一个二维数组,它的第三维的大小是多少。结果是 IndexOutOfRangeException。这是预期的行为。

在您的代码示例中,您似乎可能会混淆每个指定维度的大小与维度数。以下是不同维度的矩形数组的一些示例:

var d1 = new int[5];     // one dimensional array, containing 5 elements
var d2 = new int[3,3];   // two-dimensional 3x3 element array
var d3 = new int[2,2,2]; // three-dimension array of 2x2x2 elements
var d4 = new int[2,5,6,8]; // four dimensional array, of 2x5x6x8 elements

看到模式了吗?维数由您在数组声明中指定的数字数量决定。每个尺寸的大小由声明中每个数字的值确定。

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:

x.GetLength(2).ToString();   // asking for size of third dimension

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:

var d1 = new int[5];     // one dimensional array, containing 5 elements
var d2 = new int[3,3];   // two-dimensional 3x3 element array
var d3 = new int[2,2,2]; // three-dimension array of 2x2x2 elements
var d4 = new int[2,5,6,8]; // four dimensional array, of 2x5x6x8 elements

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.

蓝眼睛不忧郁 2024-09-22 06:50:18

你的矩阵只有 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.

因为看清所以看轻 2024-09-22 06:50:18

只是为了提供一个直观的示例并用我自己的话提供答案...(我一开始也无法理解为什么抛出异常):

double[,] x = new double[3, 3] ; 给出一个看起来像这样的数组:

[0,0] [1,0] [2,0]
[0,1] [1,1] [2,1]
[0,2] [1,2] [2,2]

我认为代码试图找到可能被认为是子数组的长度,其中每一行代表一个数组,而该数组又是一个更大数组的成员。

实际上,行本身并不是数组,只是原始数组的成员(双精度)。 GetLength 方法正在查找数组的维度而不是长度(这可能会令人困惑,因为 Length 用于确定数组中成员的数量)。如果您愿意的话,该数组有两个维度:X 维度和 Y 维度。

将其想象为一张具有两个维度的平板。如果添加第三个:

double[,,] x = new double[3, 3, 3];

工作表变成立方体,添加新维度 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:

[0,0] [1,0] [2,0]
[0,1] [1,1] [2,1]
[0,2] [1,2] [2,2]

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 because Length is used to determine the number of members in an array). This array has two dimensions, the X dimension and the Y dimension, if you will.

Think of it like a flat sheet, which has two dimensions. If you add a third:

double[,,] x = new double[3, 3, 3];

The sheet becomes a cube, a new dimension Z is added, and the third GetLength would work.

莳間冲淡了誓言ζ 2024-09-22 06:50:18
double[,,] x = new double[3, 3, 3];

MessageBox.Show(x.GetLength(0).ToString());
MessageBox.Show(x.GetLength(1).ToString());
MessageBox.Show(x.GetLength(2).ToString());
double[,,] x = new double[3, 3, 3];

MessageBox.Show(x.GetLength(0).ToString());
MessageBox.Show(x.GetLength(1).ToString());
MessageBox.Show(x.GetLength(2).ToString());
逆光飞翔i 2024-09-22 06:50:18

您的数组中没有定义 3 个维度,因此只能获得 2 个维度的长度。您的代码要求的是立方体的尺寸,而不是正方形的尺寸。

double[,] x = new double[3,3] // This sets up a two-dimensional array

MessageBox.Show(x.GetLength(0).ToString()); //Shows the length of the X axis
MessageBox.Show(x.GetLength(1).ToString()); //Shows the length of the Y axis
MessageBox.Show(x.GetLength(2).ToString()); //Trys to show the length of Z Axis.

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.

double[,] x = new double[3,3] // This sets up a two-dimensional array

MessageBox.Show(x.GetLength(0).ToString()); //Shows the length of the X axis
MessageBox.Show(x.GetLength(1).ToString()); //Shows the length of the Y axis
MessageBox.Show(x.GetLength(2).ToString()); //Trys to show the length of Z Axis.
戏剧牡丹亭 2024-09-22 06:50:18

这可以帮助您形象化所解释的内容。

您声明的 x 数组如下所示:

double[,] x = new double[3, 3];

Dimensions     0,1    0,1    0,1            
Element       [0,0], [0,1], [0,2]   
Element       [1,0], [1,1], [1,2]
Element       [2,0], [2,1], [2,2]
Lengths        3,3    3,3    3,3

This may help you visualize what's been explained.

Your array of x as you've declared it looks like this:

double[,] x = new double[3, 3];

Dimensions     0,1    0,1    0,1            
Element       [0,0], [0,1], [0,2]   
Element       [1,0], [1,1], [1,2]
Element       [2,0], [2,1], [2,2]
Lengths        3,3    3,3    3,3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文