vb.net Visual Basic 中数组的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/506207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Size of array in Visual Basic?
提问by AlexDrenea
I've tried this code in VB:
我在 VB 中试过这段代码:
Dim a(1) As Byte
Console.WriteLine(a.Length)
The output is "2". Anyone any idea why?
输出为“2”。有人知道为什么吗?
回答by AlexDrenea
If you are used to C/C++/C# languages you are used that when declaring an array to initialize it with the number of elementsin the array.
如果您习惯于 C/C++/C# 语言,则在声明数组时会使用该语言以使用数组中的元素数对其进行初始化。
C# : byte a[] = new byte[1]
will declare a byte array with 1 element (upperBound = 0)
将声明一个包含 1 个元素的字节数组(upperBound = 0)
The behavior is different in VB where, when declaring an array the parameter used in initialization represents the UpperBoundof the array.
VB 中的行为不同,其中在声明数组时,初始化中使用的参数表示数组的UpperBound。
VB.NET: Dim a(1) As Byte
will declare a byte array with 2 elements (upperBound = 1)
将声明一个包含 2 个元素的字节数组(upperBound = 1)
回答by Michael Buen
In Visual Basic, the size of an array is declared with the array's upper bound, where most languages declare the size of an array by specifying the number of elementsin the array. If you aren't aware of this, then your Visual Basic arrays end up being 1 element longer than you expected:
在 Visual Basic 中,数组的大小是用数组的上限声明的,其中大多数语言通过指定数组中元素的数量来声明数组的大小。如果您不知道这一点,那么您的 Visual Basic 数组最终会比您预期的长 1 个元素:
VB.NET:
VB.NET:
Dim a(1) as Byte ' under the hood, translated to byte[2]
Console.WriteLine("{0}", a.Length) ' output 2
a(0) = 7 ' No error, element exists
a(1) = 7 ' No error, element exists, array length is 2
a(a.Length) = 7 ' error: Index was outside the bounds of the array.
C#:
C#:
byte[] a = new byte[1];
Console.WriteLine("{0}", a.Length); // output 1
a[0] = 7 // No error, element exists
a[1] = 7 // error: Index was outside of bounds of the array. (because array length is 1)
a[a.Length] = 7; // error: Index was outside the bounds of the array.
The reason why Microsoft designed VB.NET to size arrays based on upper bound rather than array length is to make it easier to port code from VB6 to VB.NET. The initial index of a VB6 array is 1, unless you declare Option Base 0. It was common to loop through an array of size N using For i = 1 To N
. By designing VB.NET to interpret an array's sizing argument as an upper bound rather than the number of elements in the array, old VB6 code that looped from 1 to N could be ported directly to VB.NET. The array in VB.NET will have one extra element compared to what the array had in VB6 (the element at index 0) but otherwise behaves as it did in VB6.
Microsoft 将 VB.NET 设计为基于上限而不是数组长度来调整数组大小的原因是为了更容易将代码从 VB6 移植到 VB.NET。除非您声明 Option Base 0,否则 VB6 数组的初始索引为 1。通常使用 循环遍历大小为 N 的数组For i = 1 To N
。通过将 VB.NET 设计为将数组的大小参数解释为上限而不是数组中的元素数量,从 1 到 N 循环的旧 VB6 代码可以直接移植到 VB.NET。与 VB6 中的数组(索引 0 处的元素)相比,VB.NET 中的数组将有一个额外的元素,但其他方面的行为与 VB6 中的一样。
You'll sometimes see people claim that Visual Basic creates a "wasted" element. This is only true when porting legacy VB6 code which didn't expect an element at index 0. When writing new code, you just have to remember what the sizing parameter means (upper bound, not element count), and declare your arrays accordingly. Just reduce your sizing parameters by one compared to what you'd see in C#. The resulting array will have elements from a(0)
to a(a.Length-1)
, just like a C# array.
您有时会看到人们声称 Visual Basic 创建了一个“浪费”的元素。这仅在移植不期望索引为 0 的元素的遗留 VB6 代码时才是正确的。在编写新代码时,您只需要记住大小参数的含义(上限,而不是元素计数),并相应地声明您的数组。与您在 C# 中看到的相比,只需将您的大小参数减少一。生成的数组将包含从a(0)
to 的元素a(a.Length-1)
,就像 C# 数组一样。
回答by SMB
Array starts from position 0. You are defining two positions.
数组从位置 0 开始。您正在定义两个位置。
If you want only 1 position, then:
如果您只想要 1 个位置,则:
Dim a(0) As Byte
and you will get a.Length as 1.
你会得到 a.Length 为 1。
回答by Nescio
Dimension LengthThe index of each dimension is 0-based, which means it ranges from 0 through its upper bound. Therefore, the length of a given dimension is greater by 1 than the declared upper bound for that dimension.
维度长度每个维度的索引都是从 0 开始的,这意味着它的范围是从 0 到其上限。因此,给定维度的长度比该维度声明的上限大 1。
回答by Taegost
The previous answers each have pieces of the correct answer, but not the full correct answer. When you declare an array (Like with your code: Dim a(1) As Byte) the number you put in the array declaration (in this case, 1) is NOT a declaration of how manyentries in the array, it is a declaration of the upper boundaryof the array.
前面的答案每个都有正确答案的片段,但不是完整的正确答案。当您声明一个数组时(就像您的代码:Dim a(1) As Byte),您放入数组声明中的数字(在本例中为 1)不是数组中有多少条目的声明,它是一个声明数组的上边界。
So, in your declaration, you're creating an array with 2 entries: a(0) and a(1)
因此,在您的声明中,您正在创建一个包含 2 个条目的数组:a(0) 和 a(1)