Array.Copy: Copy elements by position
Public Class Tester
Public Shared Sub Main
Dim arrayA() As String = {"One", "Two", "Three", "Four", "Five", "Six"}
Console.WriteLine(Join(arrayA, ","))
Dim arrayB() As String = {"A", "B", "C", "D", "E", "E", "F", "G", "H"}
Console.WriteLine(Join(arrayB, ","))
" ----- Make a reference copy.
Dim arrayC() As String = arrayA
Console.WriteLine(Join(arrayC, ","))
" ----- Copy elements by position.
Array.Copy(arrayB, 0, arrayA, 1, 3)
Console.WriteLine(Join(arrayA, ","))
End Sub
End Class
One,Two,Three,Four,Five,Six
A,B,C,D,E,E,F,G,H
One,Two,Three,Four,Five,Six
One,A,B,C,Five,Six
Array.CreateInstance(GetType(Integer), 11)
public class Test
public Shared Sub Main
Dim values As Array = _
Array.CreateInstance(GetType(Integer), 11)
For i As Integer = 0 To 10
values.SetValue(i, i)
Next i
End Sub
End class
Array.CreateInstance(GetType(String), 10)
Option Strict On
Imports System.Collections.Generic
Public Module TypeNameFunction
Public Sub Main()
Console.WriteLine(TypeName(2 + 1.34)) " displays Double
Dim arr1 As Array = Array.CreateInstance(GetType(String), 10)
Console.WriteLine(TypeName(arr1))
Dim stringArray(10) As String
Console.Writeline(TypeName(stringArray))
Dim sl As New SortedList(Of String, String)
Console.WriteLine(TypeName(sl))
End Sub
End Module
Double
String()
String()
SortedList(Of String,String)
Array.GetLength, Array.GetUpperBound
public class Test
public Shared Sub Main
Dim aryStatistics(14, 4) As Integer
Console.WriteLine("RANK: " & aryStatistics.Rank) "Dimensions in array.
Console.WriteLine("TOTAL ELEMENTS: " & aryStatistics.Length) "Total elements in array.
Console.WriteLine("ELEMENTS IN FIRST: " & aryStatistics.GetLength(0)) "Elements in first dimension.
Console.WriteLine("ELEMENTS IN SECOND: " & aryStatistics.GetLength(1)) "Elements in second dimension.
Console.WriteLine("LAST INDEX IN FIRST: " & aryStatistics.GetUpperBound(0)) "Last index in the first dimension.
Console.WriteLine("LAST INDEX IN SECOND: " & aryStatistics.GetUpperBound(1)) "Last index in the second dimension.
End Sub
End class
RANK: 2
TOTAL ELEMENTS: 75
ELEMENTS IN FIRST: 15
ELEMENTS IN SECOND: 5
LAST INDEX IN FIRST: 14
LAST INDEX IN SECOND: 4
Array.IndexOf
public class Test
public Shared Sub Main
Dim values(10) As Integer
For i As Integer = 0 To 10
values(i) = i
Next i
Console.WriteLine(Array.IndexOf(values, 6).ToString)
End Sub
End class
6
Array.LastIndexOf
public class Test
public Shared Sub Main
Dim values(10) As Integer
For i As Integer = 0 To 10
values(i) = i
Next i
Console.WriteLine(Array.LastIndexOf(values, 3).ToString)
End Sub
End class
3
Array SetValue and GetValue
Imports System.Collections.Generic
public class Test
public Shared Sub Main
Dim squares As Array = Array.CreateInstance(GetType(Integer), 11)
For i As Integer = 0 To 10
squares.SetValue(i * i, i)
Next i
For i As Integer = 0 To 10
Console.WriteLine(squares.GetValue(i).ToString)
Next i
End Sub
End class
0
1
4
9
16
25
36
49
64
81
100
Copy array elements from one array to another
Module Tester
Sub Main()
Dim Values() As Integer = {100, 200, 300, 400, 500}
Dim MyValues(5) As Integer
Dim Prices() As Double = {25.5, 4.95, 33.4}
Dim I As Integer
For I = 0 To 4
Console.Write(Values(I) & " ")
Next
Console.WriteLine()
" Copy one array to another
Values.CopyTo(MyValues, 0)
For I = 0 To 4
Console.Write(MyValues(I) & " ")
Next
End Sub
End Module
100 200 300 400 500
100 200 300 400 500
Get Enumerator from array
Imports System.Collections
Module Module1
Sub Main()
Dim StringArray() As String = {"This", "is", "a", "test"}
Dim E As IEnumerator = StringArray.GetEnumerator()
While (E.MoveNext())
Console.WriteLine(E.Current())
End While
Dim AnArray(5) As Array
AnArray(0) = StringArray
Console.WriteLine(CType(AnArray(0), String())(0))
End Sub
End Module
This
is
a
test
This
One dimensional Array with Array.CreateInstance
public class Test
public Shared Sub Main
Dim start_time As DateTime
Dim stop_time As DateTime
Dim elapsed_time As TimeSpan
Dim num_items As Integer = 10000
" One-dimensional Array.
Dim array3 As Array = _
Array.CreateInstance(GetType(Integer), num_items)
start_time = Now
For i As Integer = 0 To num_items - 1
array3.SetValue(i, i)
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Console.WriteLine(elapsed_time.TotalSeconds())
Erase array3
End Sub
End class
0
Reverse array elements
Module Tester
Sub Main()
Dim Values() As Integer = {100, 200, 300, 400, 500}
Dim MyValues(5) As Integer
Dim Prices() As Double = {25.5, 4.95, 33.4}
Dim I As Integer
For I = 0 To 4
Console.Write(Values(I) & " ")
Next
Console.WriteLine()
Values.Reverse(Values)
For I = 0 To 4
Console.Write(Values(I) & " ")
Next
Console.WriteLine()
End Sub
End Module
100 200 300 400 500
500 400 300 200 100
Two-dimensional Array with Array.CreateInstance
public class Test
public Shared Sub Main
Dim start_time As DateTime
Dim stop_time As DateTime
Dim elapsed_time As TimeSpan
Dim num_items As Integer = 10000
" 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())
Erase array4
End Sub
End class
0
Use Array.Sort() to sort object array
Imports System
Module MyModule
Sub Main()
Dim salaries(4) As Salary
salaries(0) = New Salary(9)
salaries(1) = New Salary(4)
salaries(2) = New Salary(8)
salaries(3) = salaries(2)
salaries(4) = New Salary(6)
Console.WriteLine("Unsorted array:")
Dim salary As Salary
For Each salary In salaries
Console.WriteLine("{0}", salary)
Next
Array.Sort(salaries)
Console.WriteLine(vbCrLf & "Sorted array:")
For Each salary In salaries
Console.WriteLine("{0}", salary)
Next
End Sub
End Module
Structure Salary
Implements IComparable
Private value As Integer
Public Sub New(ByVal amount As Double)
Me.value = CInt(amount * 100)
End Sub
Public Function CompareTo(ByVal other As Object) As Integer Implements IComparable.rupareTo
Dim m2 As Salary = CType(other, Salary)
If Me.value < m2.value Then
Return -1
ElseIf Me.value = m2.value Then
Return 0
Else
Return +1
End If
End Function
Public Overrides Function ToString() As String
Return Me.value
End Function
End Structure
Unsorted array:
900
400
800
800
600
Sorted array:
400
600
800
800
900