vb.net 声明二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26111008/
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
declaring two-dimensional array
提问by JohnB
I have a couple of college assignments I am having trouble with. Really I am just confused about one thing regarding an array. I need to declare a three column, 5 row array. The first two columns are integers and the third column is the letter grade. So I am very confused about declaring the data type since they are different. This is my first go-around with arrays, so please excuse my ignorance. Here is an what my array is supposed to look like.
我有几个大学作业遇到了麻烦。真的,我只是对关于数组的一件事感到困惑。我需要声明一个三列 5 行的数组。前两列是整数,第三列是字母等级。所以我对声明数据类型很困惑,因为它们是不同的。这是我第一次使用数组,所以请原谅我的无知。这是我的数组应该是什么样子。
Column 1 {0,300,350,400,450}
Column 2 {299,349,399,449,500}
Column 3 {F,D,C,B,A}
(It's a grading app)
(这是一个评分应用程序)
I can solve the rest of the problem myself, I am just confused about this array portion. So my question is strictly about how to declare such an array. It say's to use a two-dimensional array which only confuses me more since there are three columns. Thank you!
我可以自己解决其余的问题,我只是对这个数组部分感到困惑。所以我的问题是关于如何声明这样一个数组。它说使用二维数组只会让我更加困惑,因为有三列。谢谢!
回答by SSS
2-dimensional array is correct. First index is column, second index is row.
二维数组是正确的。第一个索引是列,第二个索引是行。
Dim strData(,) As String 'Use String variable type, even for the numbers
Dim intRowCount As Integer = 5
Dim intColumnCount As Integer = 3
ReDim strData(intColumnCount - 1, intRowCount - 1) 'subtract 1 because array indices are 0-based. Column 0 = Range start, Column 1 = Range End, Column 2 = Grade
'first row
strData(0, 0) = "0" 'Range start
strData(1, 0) = "299" 'Range end
strData(2, 0) = "F" 'Grade
'second row
strData(0, 1) = "300"
strData(1, 1) = "349"
strData(2, 1) = "D"
'third row
strData(0, 2) = "350"
strData(1, 2) = "399"
strData(2, 2) = "C"
'fourth row
strData(0, 3) = "400"
strData(1, 3) = "449"
strData(2, 3) = "B"
'fifth row
strData(0, 4) = "450"
strData(1, 4) = "500"
strData(2, 4) = "A"
'Add a row
intRowCount = intRowCount + 1
ReDim Preserve strData(intColumnCount - 1, intRowCount - 1)
'sixth row
strData(0, 5) = "501"
strData(1, 5) = "600"
strData(2, 5) = "A+"
Note that Redim Preserve
can only change the last index in the array, which is why we store in (column, row)
order rather than the more traditional (row, column)
order.
请注意,Redim Preserve
只能更改数组中的最后一个索引,这就是为什么我们按(column, row)
顺序存储而不是更传统的(row, column)
顺序。
回答by xpda
There are a couple of ways to approach this. One is to declare the array as Object type, and they assign integers or strings to the appropriate element. Some don't consider this socially acceptable, though, because it can lead to code that's difficult to debug.
有几种方法可以解决这个问题。一种是将数组声明为 Object 类型,并将整数或字符串分配给适当的元素。不过,有些人不认为这在社会上是可以接受的,因为它会导致代码难以调试。
You could also use a String type for a two dimensional array and save the integers in string variables. This is also not normally done because of the conversion necessary for numeric comparisons and computation.
您还可以将 String 类型用于二维数组,并将整数保存在字符串变量中。由于数值比较和计算所需的转换,这通常也不会进行。
Another approach is to use a structure or class that contains the three values, and make an array of that.
另一种方法是使用包含三个值的结构或类,并创建一个数组。
For example,
例如,
Structure Item
Dim col1 as integer
Dim col2 as integer
Dim col3 as string
End Structure
Dim itemList(20) as Item
itemList(4).col1 = 23
itemList(4).col2 = 45
itemList(4).col3 = "somestring"