當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Array.Copy方法代碼示例

本文整理匯總了VB.NET中System.Array.Copy方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Array.Copy方法的具體用法?VB.NET Array.Copy怎麽用?VB.NET Array.Copy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Array的用法示例。


在下文中一共展示了Array.Copy方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Main

Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array of type Int32.
        Dim myIntArray As Array = _
           Array.CreateInstance(GetType(System.Int32), 5)
        Dim i As Integer
        For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
            myIntArray.SetValue(i + 1, i)
        Next i 
        ' Creates and initializes a new Array of type Object.
        Dim myObjArray As Array = _
           Array.CreateInstance(GetType(System.Object), 5)
        For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0)
            myObjArray.SetValue(i + 26, i)
        Next i 
        ' Displays the initial values of both arrays.
        Console.WriteLine("Int32 array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array:")
        PrintValues(myObjArray)
        
        ' Copies the first element from the Int32 array to the Object array.
        Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _
           myObjArray.GetLowerBound(0), 1)
        
        ' Copies the last two elements from the Object array to the Int32 array.
        Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
           myIntArray.GetUpperBound(0) - 1, 2)
        
        ' Displays the values of the modified arrays.
        Console.WriteLine("Int32 array - Last two elements should now be " _
           + "the same as Object array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array - First element should now be the " _
           + "same as Int32 array:")
        PrintValues(myObjArray)
    End Sub
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:57,代碼來源:Array.Copy

輸出:

Int32 array:
1    2    3    4    5
Object array:
26    27    28    29    30
Int32 array - Last two elements should now be the same as Object array:
1    2    3    29    30
Object array - First element should now be the same as Int32 array:
1    27    28    29    30

示例2: MainClass

' 導入命名空間
Imports System
Imports System.Collections

Public Class MainClass
   Shared Private integerValues As Integer() = {1, 2, 3, 4, 5, 6}

   Shared Private integerValuesCopy(6) As Integer

   Public Shared Sub Main()
      Dim result As Integer

      Console.WriteLine("Initial Array Values:" )
      PrintArray()

      Array.Copy(integerValues, integerValuesCopy,integerValues.Length)

      Console.WriteLine("Array values after Copy:" )

      PrintArray() 

      result = Array.BinarySearch(integerValues, 5)

      If result >= 0 Then
         Console.WriteLine("5 found at element " & result )
      Else
         Console.WriteLine("5 not found" & " in integerValues")
      End If

   End Sub
   Shared Private Sub PrintArray()
      Dim integerElement As Integer

      For Each integerElement In integerValues
         Console.WriteLine(integerElement )
      Next

      Console.WriteLine(" integerValuesCopy: ")

      For Each integerElement In integerValuesCopy
         Console.WriteLine(integerElement )
      Next

   End Sub

End Class
開發者ID:VB程序員,項目名稱:System,代碼行數:46,代碼來源:Array.Copy


注:本文中的System.Array.Copy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。