获取交错数组中的元素总数
Get total number of elements in jagged array
我声明了一个字节数组:
Dim commands As Byte()()
该数组后来填充了一堆字节值,在我当前的测试中它产生了一个矩形 3x3 数组(直线性是巧合,该数组可以包含任何长度的字节数组)。
commands = New Byte(3)(){}
'...
commands(i) = GetBytes(x)
稍后我仍然想获得锯齿状数组中的总字节数。我假设 Array.Length
会像描述中所说的那样执行此操作
The total number of elements in all the dimensions of the Array
不过,似乎实际上只是returning 4;数组第一维的计数,即 GetLength(0)
或行数。 Array.Count
也 return 相同(即 3)。
我是否正确理解了 Array.Length
应该 return 的内容?如果我有,那为什么不是 returning 9?
P.S: 我试过其他大小的数组,Length
肯定是 returning GetLength(0)
Have I correctly understood what Array.Length should return? If I have, then why is it not returning 9?
不完全是。顶级数组 只有 3 个元素,这就是它的 Length
属性 所报告的内容。只是这些元素恰好 也 是数组。
您需要循环(或编写 linq 查询等)来获取所有数组的总计数,例如:
Dim total as Integer = 0
For Each subarray as Byte() In commands
total += subarray.Length
Next
你在这里遇到的另一个问题是你有一个数组数组,而不是一个多维数组,它会被声明为:
Dim commands as Byte(,)
现在这是一个 单个 数组,其中 Length
属性 会 return 你 all 个元素。
我声明了一个字节数组:
Dim commands As Byte()()
该数组后来填充了一堆字节值,在我当前的测试中它产生了一个矩形 3x3 数组(直线性是巧合,该数组可以包含任何长度的字节数组)。
commands = New Byte(3)(){}
'...
commands(i) = GetBytes(x)
稍后我仍然想获得锯齿状数组中的总字节数。我假设 Array.Length
会像描述中所说的那样执行此操作
The total number of elements in all the dimensions of the Array
不过,似乎实际上只是returning 4;数组第一维的计数,即 GetLength(0)
或行数。 Array.Count
也 return 相同(即 3)。
我是否正确理解了 Array.Length
应该 return 的内容?如果我有,那为什么不是 returning 9?
P.S: 我试过其他大小的数组,Length
肯定是 returning GetLength(0)
Have I correctly understood what Array.Length should return? If I have, then why is it not returning 9?
不完全是。顶级数组 只有 3 个元素,这就是它的 Length
属性 所报告的内容。只是这些元素恰好 也 是数组。
您需要循环(或编写 linq 查询等)来获取所有数组的总计数,例如:
Dim total as Integer = 0
For Each subarray as Byte() In commands
total += subarray.Length
Next
你在这里遇到的另一个问题是你有一个数组数组,而不是一个多维数组,它会被声明为:
Dim commands as Byte(,)
现在这是一个 单个 数组,其中 Length
属性 会 return 你 all 个元素。