VB.Net/Data Structure/Array
Содержание
- 1 Array IndexOf and LastIndexOf
- 2 Array Length Property
- 3 Array Performance Test: One-dimensional array
- 4 Array Performance Test: SetValue(i, i)
- 5 Array UBound
- 6 Array Upper Bound and Lower Bound
- 7 A simple class to store in the array
- 8 Bounded array Example
- 9 Declare an Array and reference its member
- 10 Declaring, allocating and initializing arrays
- 11 For Each loops through Array
- 12 Free array"s memory
- 13 Init an Array in Declaration
- 14 Reference an Element in an Array by Index
- 15 Set Array Element Value by Index
- 16 Store your won Class in an Array
- 17 Two ways to loop through Array
- 18 Use Array CreateInstance to Create Array
- 19 Use For Each/Next to find a minimum grade
Array IndexOf and LastIndexOf
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim values(10) As Integer
For i As Integer = 0 To 10
values(i) = i
Next i
Console.WriteLine(Array.IndexOf(values, 6).ToString)
Console.WriteLine(Array.LastIndexOf(values, 3).ToString)
End Sub
End Class
Array Length Property
Imports System
Imports System.Diagnostics
Public Class MainClass
Shared Sub Main()
Dim arr(99) As Integer
For i As Integer = 0 To 99
arr(i) = i * i
Next i
Console.WriteLine(arr.Length)
End Sub
End Class
Array Performance Test: One-dimensional array
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 = 10000
" One-dimensional array.
Dim array1(0 To num_items - 1) As Integer
start_time = Now
For i As Integer = 0 To num_items - 1
array1(i) = i
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Console.WriteLine( elapsed_time.TotalSeconds().ToString )
End Sub
End Class
Array Performance Test: SetValue(i, i)
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
" 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().ToString )
End Sub
End Class
Array UBound
Public Class MainClass
Public Shared Sub Main()
Dim arrMyIntArray1(20) As Integer
Dim arrMyIntArray2() As Integer = {1, 2, 3, 4}
Dim arrMyIntArray3(4, 2) As Integer
Dim arrMyIntArray4(,) As Integer = _
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}
Dim arrMyIntArray5() As Integer
Console.WriteLine(CStr(UBound(arrMyIntArray2)))
arrMyIntArray2.GetUpperBound(0)
Dim intLoop1 As Integer
Dim intLoop2 As Integer
For intLoop1 = 0 To UBound(arrMyIntArray4)
For intLoop2 = 0 To UBound(arrMyIntArray4, 2)
Console.WriteLine(arrMyIntArray4(intLoop1, intLoop2).ToString)
Next
Next
End Sub
End Class
Array Upper Bound and Lower Bound
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim friends() As String = {"1", "2", "3","4", "5"}
Console.WriteLine("Upper bound: " & UBound(friends), "Array Demo")
Console.WriteLine("Lower bound: " & LBound(friends), "Array Demo")
Array.Sort(friends)
Dim upperBound As Integer = friends.Length
Dim random As New System.Random()
Dim n As Integer
For n = 1 To 10
Dim index As Integer = random.Next(upperBound)
Console.WriteLine(index & ": " & friends(index))
Next
End Sub
End Class
A simple class to store in the array
Imports System
Public Class MainClass
Shared Sub Main()
Dim intArray As Integer( )
Dim empArray As Employee( )
intArray = New Integer(5) {}
empArray = New Employee(3) {}
"populate the array
Dim i As Integer
"for indices 0 through 3
For i = 0 To empArray.Length - 1
empArray(i) = New Employee(i + 5)
i = i + 1
Next
End Sub
End Class
Public Class Employee
Private empID As Integer
"constructor
Public Sub New(ByVal empID As Integer)
Me.empID = empID
End Sub
End Class
Bounded array Example
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim X As Array
Dim I As Integer
Dim Lengths(0) As Integer
Dim LowerBounds(0) As Integer
Lengths(0) = 10
LowerBounds(0) = 1
X = Array.CreateInstance(GetType(System.Int32), Lengths, LowerBounds)
Try
X.SetValue(5, 0)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
X.SetValue(6, 1)
Console.WriteLine(X.GetValue(1))
End Sub
End Class
Declare an Array and reference its member
Imports System
Public Class MainClass
Shared Sub Main()
"Declare an array
Dim strFriends(4) As String
"Populate the array
strFriends(0) = "R"
strFriends(1) = "B"
strFriends(2) = "S"
strFriends(3) = "S"
strFriends(4) = "K"
"Add the first array item to the list
System.Console.WriteLine(strFriends(0))
End Sub
End Class
Declaring, allocating and initializing arrays
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim i As Integer
Dim array As Integer() " declare array variable
Dim array1, array2 As Integer() " declare two arrays
array = New Integer(9) {} " allocate memory for array
" initializer list specifies number of elements
" and value of each element
array1 = New Integer() {32, 27, 64, 18, 95, _
14, 90, 70, 60, 37}
" allocate array2 based on length of array1
array2 = New Integer(array1.GetUpperBound(0)) {}
" set values in array2 by a calculation
For i = 0 To array2.GetUpperBound(0)
array2(i) = 2 + 2 * i
Next
Console.WriteLine( "Subscript " & vbTab & "Array" & vbTab & _
"Array1" & vbTab & "Array2" & vbCrLf )
" display values in array
For i = 0 To array.GetUpperBound(0)
Console.WriteLine( i & vbTab & array(i) & vbTab & _
array1(i) & vbTab & array2(i) & vbCrLf )
Next
Console.WriteLine( vbCrLf & "The array contains " & _
array.Length & " elements." )
End Sub
End Class
For Each loops through Array
Imports System
Public Class MainClass
Shared Sub Main()
Dim intArray As Integer( ) = {2, 4, 6, 8, 10}
Dim empArray As Employee( ) = _
{New Employee(5), New Employee(7), New Employee(9)}
Console.WriteLine("The Integer array...")
Dim theInt As Integer
For Each theInt In intArray
Console.WriteLine(theInt.ToString( ))
Next theInt
Console.WriteLine("The Employee array...")
Dim e As Employee
For Each e In empArray
Console.WriteLine(e.ToString( ))
Next e
End Sub
End Class
Public Class Employee
Private empID As Integer
"constructor
Public Sub New(ByVal empID As Integer)
Me.empID = empID
End Sub
End Class
Free array"s memory
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
" Get the number of items.
Dim num_items As Integer = 10000
" One-dimensional array.
Dim array1(0 To num_items - 1) As Integer
For i As Integer = 0 To num_items - 1
array1(i) = i
Next i
" Free the first array"s memory.
Erase array1
End Sub
End Class
Init an Array in Declaration
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim friends() As String = {"1", "2", "3","4", "5"}
Dim upperBound As Integer = friends.Length
Dim random As New System.Random()
Dim n As Integer
For n = 1 To 10
Dim index As Integer = random.Next(upperBound)
Console.WriteLine(index & ": " & friends(index))
Next
End Sub
End Class
Reference an Element in an Array by Index
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim friends(4) As String
friends(0) = "1"
friends(1) = "2"
friends(2) = "3"
friends(3) = "4"
friends(4) = "5"
Console.WriteLine(friends(2))
End Sub
End Class
Set Array Element Value by Index
Imports System
Public Class MainClass
Shared Sub Main()
Dim anArray As Array
Dim l(0) As Integer
Dim lowerBounds(0) As Integer
l(0) = 7
lowerBounds(0) = 1995
"creates an array of objects numbered 1995 - 2002
anArray = Array.CreateInstance(GetType(System.Int32), l, lowerBounds)
anArray.SetValue(200000, 1995)
anArray.SetValue(1000000, 2001)
Console.WriteLine("The entry in position 1995 is " & _
(anArray.GetValue(1995).ToString))
Console.WriteLine("The entry in position 2002 is " & _
(anArray.GetValue(2001).ToString))
End Sub
End Class
Store your won Class in an Array
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim intArray As Integer()
Dim empArray As Employee()
intArray = New Integer(5) {}
empArray = New Employee(3) {}
" populate the array
Dim i As Integer
" for indices 0 through 3
For i = 0 To empArray.Length - 1
empArray(i) = New Employee(i + 5)
i = i + 1
Next
End Sub
End Class
Public Class Employee
Private empID As Integer
" constructor
Public Sub New(ByVal empID As Integer)
Me.empID = empID
Console.WriteLine(empID)
End Sub
End Class
Two ways to loop through Array
Imports System
Public Class MainClass
Shared Sub Main()
Dim intArray( ) As Integer
Dim empArray( ) As Employee
intArray = New Integer(5) {}
empArray = New Employee(3) {}
"populate the array
Dim i As Integer
For i = 0 To empArray.Length - 1
empArray(i) = New Employee(i + 5)
Next i
Console.WriteLine("The Integer array...")
Dim intValue As Integer
For Each intValue In intArray
Console.WriteLine(intValue.ToString( ))
Next
Console.WriteLine("The Employee array...")
Dim e As Employee
For Each e In empArray
Console.WriteLine(e)
Next
End Sub
End Class
Public Class Employee
Private empID As Integer
"constructor
Public Sub New(ByVal empID As Integer)
Me.empID = empID
End Sub
End Class
Use Array CreateInstance to Create Array
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim values As Array = _
Array.CreateInstance(GetType(Integer), 11)
For i As Integer = 0 To 10
values.SetValue(i, i)
Next i
Console.WriteLine(Array.IndexOf(values, 6).ToString)
Console.WriteLine(Array.LastIndexOf(values, 3).ToString)
End Sub
End Class
Use For Each/Next to find a minimum grade
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim gradeArray As Integer(,) = New Integer(,) {{1, 2, 3, 4}, {4, 3, 8, 1}, {7, 9, 8, 1}}
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
End Class