VB.Net/Data Structure/Array Multi Dimension

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

Array Performance Test: Two-dimensional array

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main(ByVal args As String())
       Dim start_time As DateTime
       Dim stop_time As DateTime
       Dim elapsed_time As TimeSpan
       " Get the number of items.
       Dim num_items As Integer = 1000000
       " Two-dimensional array.
       Dim array2(0 To 0, 0 To num_items - 1) As Integer
       start_time = Now
       For i As Integer = 0 To num_items - 1
           array2(0, i) = i
       Next i
       stop_time = Now
       elapsed_time = stop_time.Subtract(start_time)
       Console.WriteLine( elapsed_time.TotalSeconds().ToString )
   End Sub

End Class


      </source>


Array Performance Test: Two-dimensional Array and SetValue(i, i)

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main(ByVal args As String())
       Dim start_time As DateTime
       Dim stop_time As DateTime
       Dim elapsed_time As TimeSpan
       " Get the number of items.
       Dim num_items As Integer = 1000000
       " Two-dimensional Array.
       Dim array4 As Array = _
           Array.CreateInstance(GetType(Integer), 1, num_items)
       start_time = Now
       For i As Integer = 0 To num_items - 1
           array4.SetValue(i, 0, i)
       Next i
       stop_time = Now
       elapsed_time = stop_time.Subtract(start_time)
       Console.WriteLine( elapsed_time.TotalSeconds().ToString )
   End Sub

End Class


      </source>


Define and initialize the multi-dimensional array

<source lang="vbnet"> Imports System Public Class MainClass

   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}}
    "report the contents of the array
    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

End Class


      </source>


Define and Init two dimensional array

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
            Const rowsUB As Integer = 4
            Const columnsUB As Integer = 3
            " define and initialize the array
            Dim rectangularArray As Integer(,) = _
            {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}
            " report the contents of the array
            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

End Class

      </source>


Initializing a jagged array, one in which the length of each array differs

<source lang="vbnet"> Imports System Imports System.Data Imports System.Collections public class MainClass

  Shared Sub Main()
       Dim c()() As Integer = {New Integer() {1, 2}, New Integer() {1, 2, 3}}
       c(1) = New Integer() {4, 5}
       Console.WriteLine(c(1)(1))
       c(1) = System.Array.CreateInstance(GetType(System.Int32), 10)
       Console.WriteLine(c(1)(1))
       Dim d() As Integer = {6, 7}
       c(1) = d
       Console.WriteLine(c(1)(1))
  End Sub

End Class


      </source>


Initializing multidimensional arrays: rectangular two-dimensional array

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
     Dim i, j As Integer
     " 
     Dim array1 As Integer(,)
     array1 = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
     Console.WriteLine( "Values in array1 by row are " )
     For i = 0 To array1.GetUpperBound(0)
        For j = 0 To array1.GetUpperBound(1)
           Console.WriteLine( array1(i, j) ) 
        Next
     Next
   End Sub

End Class

      </source>


Jagged two-dimensional array

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
     Dim i, j As Integer
     " 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}
     Console.WriteLine( "Values in array2 by row are ")
     For i = 0 To array2.GetUpperBound(0)
        For j = 0 To array2(i).GetUpperBound(0)
           Console.WriteLine( array2(i)(j) )
        Next
     Next
   End Sub

End Class

      </source>


Seclare a 4x3 Integer array

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main()
            Const rowsUB As Integer = 4
            Const columnsUB As Integer = 3
            Dim rectangularArray(rowsUB, columnsUB) As Integer
            "populate the array
            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
            "report the contents of the array
            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


      </source>


Two Dimensional Rectangle Array

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
        Const rowsUB As Integer = 4
        Const columnsUB As Integer = 3
        " declare a 4x3 Integer array
        Dim rectangularArray(rowsUB, columnsUB) As Integer
        " populate the array
        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
        " report the contents of the array
        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

      </source>


Two dimension array Demo

<source lang="vbnet"> Imports System Imports System.Diagnostics Public Class MainClass

   Shared Sub Main()
       Dim int_values(,) As Integer = _
       { _
           {1, 2, 3}, _
           {4, 5, 6} _
       }
       For i As Integer = 0 To 1
           For j As Integer = 0 To 2
               Console.Write(int_values(i, j))
           Next j
           Console.WriteLine("")
       Next i
       Dim str_values(,,) As String = _
       { _
           { _
               {"000", "001", "002"}, _
               {"010", "011", "012"} _
           }, _
           { _
               {"100", "101", "102"}, _
               {"110", "111", "112"} _
           } _
       }
       Console.WriteLine("")
       For i As Integer = 0 To 1
           For j As Integer = 0 To 1
               For k As Integer = 0 To 2
                   Console.Write(str_values(i, j, k) & " ")
               Next k
           Next j
           Console.WriteLine("")
       Next i
   End Sub

End Class


      </source>