当前位置: 首页>>代码示例>>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;未经允许,请勿转载。