VB.Net Tutorial/Collections/Array Bound — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:53, 26 мая 2010
Содержание
Array lower bound and upper bound
<source lang="vbnet">Option Strict On Public Class CArray
Public Shared Sub Main() Dim dims() As Integer = {10} " Number of elements in array Dim bnds() As Integer = {1} " Lower bound of array Dim Scores As Array = Array.CreateInstance(GetType(Integer), dims, bnds) Console.WriteLine(UBound(Scores)) Console.WriteLine(LBound(Scores)) End Sub
End Class</source>
10 1
Get array lower bound, upper bound and length
<source lang="vbnet">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() Console.WriteLine("Array length: " & Values.Length) Console.WriteLine("Array lowerbound: " & Values.GetLowerBound(0)) Console.WriteLine("Array upperbound: " & Values.GetUpperBound(0)) End Sub
End Module</source>
100 200 300 400 500 Array length: 5 Array lowerbound: 0 Array upperbound: 4
GetUpperBound
<source lang="vbnet">Module Tester
Sub Main() Dim answer, rating As Integer Dim output As String Dim responses As Integer() responses = New Integer() {1, 2, 6, 4, 8, 5, 9, 7, _ 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, _ 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10} " response frequency array (indices 0 through 10) Dim frequency As Integer() = New Integer(10) {} " count frequencies For answer = 0 To responses.GetUpperBound(0) frequency(responses(answer)) += 1 Next Console.WriteLine("Rating " & vbTab & "Frequency ") For rating = 1 To frequency.GetUpperBound(0) Console.WriteLine(rating & vbTab & frequency(rating)) Next End Sub " Main
End Module</source>
Rating Frequency 1 2 2 2 3 2 4 2 5 5 6 11 7 5 8 7 9 1 10 3
Show and use the array boundaries
<source lang="vbnet">public class Test
public Shared Sub Main Dim aryNames(9) As String Dim intCounter As Integer "Show the array boundaries. Console.WriteLine("LOWER BOUND: " & aryNames.GetLowerBound(0)) Console.WriteLine("UPPER BOUND: " & aryNames.GetUpperBound(0)) Console.WriteLine("LENGTH: " & aryNames.Length) "Populate the array. For intCounter = 0 To aryNames.GetUpperBound(0) aryNames(intCounter) = "Element position = " & intCounter Next intCounter "Show the elements of the array. For intCounter = 0 To aryNames.GetUpperBound(0) Console.WriteLine("Element position = " & intCounter) Next intCounter
End Sub
End class</source>
LOWER BOUND: 0 UPPER BOUND: 9 LENGTH: 10 Element position = 0 Element position = 1 Element position = 2 Element position = 3 Element position = 4 Element position = 5 Element position = 6 Element position = 7 Element position = 8 Element position = 9
Use array UBound in For loop
<source lang="vbnet">Module Module1
Sub Main() Dim Scores(2) As Integer Scores(0) = 45 Scores(1) = 55 Scores(2) = 65 For intLoopIndex As Integer = 0 To UBound(Scores) Console.WriteLine("Score(" & intLoopIndex & ") = " & Scores(intLoopIndex)) Next intLoopIndex End Sub
End Module</source>
Score(0) = 45 Score(1) = 55 Score(2) = 65