声明二维数组

Joh*_*hnB 5 vb.net arrays

我有几个大学作业,我遇到了麻烦.真的我只是对阵列的一件事情感到困惑.我需要声明一个三列,五行数组.前两列是整数,第三列是字母等级.因此,我对于声明数据类型非常困惑,因为它们不同.这是我第一次使用数组,所以请原谅我的无知.这是我的数组应该是什么样子.

Column 1 {0,300,350,400,450}
Column 2 {299,349,399,449,500}
Column 3 {F,D,C,B,A}
Run Code Online (Sandbox Code Playgroud)

(这是一个评分应用程序)

我自己可以解决剩下的问题,我只是对这个数组部分感到困惑.所以我的问题严格来说是如何声明这样一个数组.它说是使用二维数组,因为有三列,所以只会让我感到困惑.谢谢!

SSS*_*SSS 13

二维数组是正确的.第一个索引是列,第二个索引是行.

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+"
Run Code Online (Sandbox Code Playgroud)

请注意,Redim Preserve只能更改数组中的最后一个索引,这就是我们按(column, row)顺序存储而不是更传统的(row, column)顺序的原因.