VB.Net Tutorial/Collections/Multiple dimensional array

Материал из VB Эксперт
Версия от 12:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Declare the jagged array as 4 rows high

Option Strict On
 Imports System
 Public Class Tester
     Public Shared Sub Main()
         Const rowsUB As Integer = 3  " upper bounds
         Const rowZero As Integer = 5
         Const rowOne As Integer = 2
         Const rowTwo As Integer = 3
         Const rowThree As Integer = 5
         Dim i As Integer
         
         Dim jaggedArray(rowsUB)() As Integer
         ReDim jaggedArray(0)(rowZero)
         ReDim jaggedArray(1)(rowOne)
         ReDim jaggedArray(2)(rowTwo)
         ReDim jaggedArray(3)(rowThree)
         jaggedArray(0)(3) = 15
         jaggedArray(1)(1) = 12
         jaggedArray(2)(1) = 9
         jaggedArray(2)(2) = 99
         jaggedArray(3)(0) = 10
         jaggedArray(3)(1) = 11
         jaggedArray(3)(2) = 12
         jaggedArray(3)(3) = 13
         jaggedArray(3)(4) = 14
         For i = 0 To rowZero
             Console.WriteLine("jaggedArray(0)({0}) = {1}", _
                 i, jaggedArray(0)(i))
         Next
         For i = 0 To rowOne
             Console.WriteLine("jaggedArray(1)({0}) = {1}", _
                 i, jaggedArray(1)(i))
         Next
         For i = 0 To rowTwo
             Console.WriteLine("jaggedArray(2)({0}) = {1}", _
                 i, jaggedArray(2)(i))
         Next
         For i = 0 To rowThree
             Console.WriteLine("jaggedArray(3)({0}) = {1}", _
                 i, jaggedArray(3)(i))
         Next
     End Sub
 End Class
jaggedArray(0)(0) = 0
jaggedArray(0)(1) = 0
jaggedArray(0)(2) = 0
jaggedArray(0)(3) = 15
jaggedArray(0)(4) = 0
jaggedArray(0)(5) = 0
jaggedArray(1)(0) = 0
jaggedArray(1)(1) = 12
jaggedArray(1)(2) = 0
jaggedArray(2)(0) = 0
jaggedArray(2)(1) = 9
jaggedArray(2)(2) = 99
jaggedArray(2)(3) = 0
jaggedArray(3)(0) = 10
jaggedArray(3)(1) = 11
jaggedArray(3)(2) = 12
jaggedArray(3)(3) = 13
jaggedArray(3)(4) = 14
jaggedArray(3)(5) = 0

Define and initialize the two dimensional array

Imports System
Class Tester
   Public Shared Sub Main()
       Const rowsUB As Integer = 4
       Const columnsUB As Integer = 3
       
       Dim rectangularArray As Integer(,) = _
       {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}
       Dim i As Integer
       For i = 0 To rowsUB - 1
           Dim j As Integer
           For j = 0 To columnsUB - 1
               Console.WriteLine( _
                "rectangularArray[{0},{1}] = {2}", _
                i, j, rectangularArray(i, j))
           Next j
       Next i
   End Sub "Run
   
End Class
rectangularArray[0,0] = 0
rectangularArray[0,1] = 1
rectangularArray[0,2] = 2
rectangularArray[1,0] = 3
rectangularArray[1,1] = 4
rectangularArray[1,2] = 5
rectangularArray[2,0] = 6
rectangularArray[2,1] = 7
rectangularArray[2,2] = 8
rectangularArray[3,0] = 9
rectangularArray[3,1] = 10
rectangularArray[3,2] = 11

Initialize two dimensional array by index

Public Class Tester
    Public Shared Sub Main
    
        Dim SqaresTable(5, 1) As Integer
    
        SqaresTable(0, 0) = 0
        SqaresTable(0, 1) = 0
        SqaresTable(1, 0) = 1
        SqaresTable(1, 1) = 1
        SqaresTable(2, 0) = 2
        SqaresTable(2, 1) = 4
        SqaresTable(3, 0) = 3
        SqaresTable(3, 1) = 9
        SqaresTable(4, 0) = 4
        SqaresTable(4, 1) = 16
        SqaresTable(5, 0) = 5
        SqaresTable(5, 1) = 25
    
        Console.WriteLine(SqaresTable(2, 1))
    
    End Sub
End Class
4

Initializing multi-dimensional arrays

Module Tester
   Sub Main()
      Dim i, j As Integer
      " create rectangular two-dimensional array
      Dim array1 As Integer(,)
      array1 = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
      " create jagged two-dimensional array
      Dim array2 As Integer()() = New Integer(2)() {}
      array2(0) = New Integer() {1, 2}
      array2(1) = New Integer() {3}
      array2(2) = New Integer() {4, 5, 6}

      For i = 0 To array1.GetUpperBound(0)
         For j = 0 To array1.GetUpperBound(1)
            Console.Write(array1(i, j) & "  ")
         Next
         Console.WriteLine("")
      Next
      For i = 0 To array2.GetUpperBound(0)
         For j = 0 To array2(i).GetUpperBound(0)
            Console.WriteLine(array2(i)(j) & "  ")
         Next
         Console.WriteLine("")
      Next
   End Sub 
End Module
1  2  3
4  5  6
1
2
3
4
5
6

Jagged two-dimensional array example.

Module Tester
   Dim lastStudent, lastExam As Integer
   Sub Main()
      Dim i As Integer
      " jagged array with 3 rows of exam scores
      Dim gradeArray As Integer()() = New Integer(2)() {}
      gradeArray(0) = New Integer() {7, 8, 6, 3}
      gradeArray(1) = New Integer() {8, 7, 9, 1}
      gradeArray(2) = New Integer() {0, 0, 6, 1}
      " upper bounds for array manipulations
      lastStudent = gradeArray.GetUpperBound(0)
      lastExam = gradeArray(0).GetUpperBound(0)

      Console.WriteLine("Lowest grade: " & _
         Minimum(gradeArray) & vbCrLf & "Highest grade: " & _
         Maximum(gradeArray))
      For i = 0 To lastStudent
         Console.WriteLine("Average for student " & _
            i & " is " & Average(gradeArray(i)))
      Next
   End Sub " Main
   Function Minimum(ByVal grades As Integer()()) _
      As Integer
      Dim lowGrade As Integer = 100
      Dim i, j As Integer
      For i = 0 To lastStudent
         For j = 0 To lastExam
            If grades(i)(j) < lowGrade Then
               lowGrade = grades(i)(j)
            End If
         Next
      Next
      Return lowGrade
   End Function " Minimum
   Function Maximum(ByVal grades As Integer()()) _
      As Integer
      Dim highGrade As Integer = 0
      Dim i, j As Integer
      For i = 0 To lastStudent
         For j = 0 To lastExam
            If grades(i)(j) > highGrade Then
               highGrade = grades(i)(j)
            End If
         Next
      Next
      Return highGrade
   End Function " Maximum
   Function Average(ByVal setOfGrades As Integer()) _
      As Double
      Dim i As Integer, total As Integer = 0
      For i = 0 To lastExam
         total += setOfGrades(i)
      Next
      Return total / setOfGrades.Length
   End Function " Average
End Module
Lowest grade: 0
Highest grade: 9
Average for student 0 is 6
Average for student 1 is 6.25
Average for student 2 is 1.75

Rectangular Array

Option Strict On
 Imports System
 Class Tester
     Shared Sub Main()
         Const rowsUB As Integer = 4
         Const columnsUB As Integer = 3
         Dim rectangularArray(rowsUB, columnsUB) As Integer
         Dim i As Integer
         For i = 0 To rowsUB - 1
             Dim j As Integer
             For j = 0 To columnsUB - 1
                 rectangularArray(i, j) = i + j
             Next j
         Next i
         For i = 0 To rowsUB - 1
             Dim j As Integer
             For j = 0 To columnsUB - 1
                 Console.WriteLine( _
                   "rectangularArray[{0},{1}] = {2}", _
                   i, j, rectangularArray(i, j))
             Next j
         Next i
     End Sub
 End Class
rectangularArray[0,0] = 0
rectangularArray[0,1] = 1
rectangularArray[0,2] = 2
rectangularArray[1,0] = 1
rectangularArray[1,1] = 2
rectangularArray[1,2] = 3
rectangularArray[2,0] = 2
rectangularArray[2,1] = 3
rectangularArray[2,2] = 4
rectangularArray[3,0] = 3
rectangularArray[3,1] = 4
rectangularArray[3,2] = 5

Reference elements in two dimensional array

public class Test
   public Shared Sub Main
        Dim aryBoard(9, 9) As Integer
        "Move the element.
        aryBoard(0, 0) = 0
        aryBoard(2, 4) = 1
        "Check for the element.
        If aryBoard(0, 0) = 1 Then
            Console.WriteLine("The piece was found.")
        Else
            Console.WriteLine("The piece was not found.")
        End If
   End Sub

End class
The piece was not found.

Set the array of arrays

public class Test
   public Shared Sub Main
        Dim aryIntArray(9) As Integer
        Dim aryStrArray(99) As String
        Dim aryBigArray(1) As Object
        Dim intCounter As Integer
        "Populate array aryIntArray.
        For intCounter = 0 To 9
            aryIntArray(intCounter) = intCounter
        Next
        "Populate array aryStrArray.
        For intCounter = 0 To 99
            aryStrArray(intCounter) = "ITEM: " & intCounter.ToString("0000")
        Next
        "Set the array of arrays.
        aryBigArray(0) = aryIntArray
        aryBigArray(1) = aryStrArray
        "Display some array values.
        Console.WriteLine(aryBigArray(0)(7))
        Console.WriteLine(aryBigArray(1)(16))
   End Sub
End class
7
ITEM: 0016

Use For Each/Next to find an element in a two dimension array

Module modForEach
   Sub Main()
      Dim gradeArray As Integer(,) = New Integer(,) _
         {{77, 68, 86, 73}, {98, 87, 89, 81}, {70, 90, 86, 81}}
      Dim grade As Integer
      Dim lowGrade As Integer = 100
      For Each grade In gradeArray
         If grade < lowGrade Then
            lowGrade = grade
         End If
      Next
      Console.WriteLine("The minimum grade is: {0}", lowGrade)
   End Sub " Main
End Module
The minimum grade is: 68