你能创建一个其他数组的数组吗?

时间:2015-05-27 17:27:37

标签: arrays vb.net

如果我有多个学生的一系列学生考试成绩(例如:每个5年级的5名学生)

Dim aStudent1Grades() As New String = {Me.tboStudent1Grade1.Text, Me.tboStudent1Grade2.Text, Me.Student1Grade3.Text, Me.Student1Grade4.Text, Me.Student1Grade5.Text}

(以同样的方式为其他4名学生创建4个其他数组)

然后我想创建一个数组并将这5个学生数组存储到其中,这样我就可以遍历它并进行所有数据验证测试。

类似的东西:

Dim aAllGrades() As New Array = {aStudent1Grades(), aStudent2Grades(),        aStudent3Grades(), aStudent4Grades(), aStudent5Grades()}

我会使用For循环遍历数组数组,这些数组将在其中包含另一个For循环,以遍历每个aStudentGrade数组来测试数据。

是否可以将数组存储在另一个数组中? 感谢

3 个答案:

答案 0 :(得分:2)

这是一个c#示例,但您应该明白这一点。

int[] array1= new int[4] { 44, 2, 3, 4};
int[] array2 = new int[4] { 55, 6, 33, 3};
int[] array3 = new int[4] { 77, 22, 4, 1 };
int[] array4 = new int[4] { 77, 4, 3, 3};

int[][] arrays= new int[][] {  array1,  array2,  array3,  array4 };

答案 1 :(得分:1)

当然 - 只需将其设为jagged array

<br>

然后你可以以强类型的方式循环:

Dim aAllGrades()() As String = {aStudent1Grades, aStudent2Grades, aStudent3Grades}

答案 2 :(得分:0)

我建议您在课堂上对学生进行分类:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create an array of 5 for the Students
        Dim Students(5) As Student  'could be a List(Of Student) so you can add a new one whenever you like without any effort
        Dim i As Integer = 0 'Just an example for useage


        For j = 0 To 5
            Students(j) = New Student
        Next

        'Add the Grades
        Students(0).Grades.Add(Me.tboStudent1Grade1.Text)
        'etc

        'An example for a loop
        For Each s In Students
            For Each g As Integer In s.Grades
                i += g 
            Next
        Next


    End Sub
End Class


Public Class Student
    Public Name As String
    Public Grades As New List(Of Integer)

    Shared Sub New()

    End Sub
End Class