VB.Net by API/System/Array — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:51, 26 мая 2010
Содержание
Array.BinarySearch
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
Array.Clone
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, ","))
" ----- Make a full, unique copy of all elements.
Dim arrayD() As String = arrayA.Clone
Console.WriteLine(Join(arrayD, ","))
End Sub
End Class
Array.Copy
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
Array.CreateInstance
Option Strict On
Public Module Arrays
Public Sub Main
Dim arr As Array
Dim int() As Integer = {12, 16, 20, 24, 28, 32}
arr = CType(int, Array)
Dim byFours() As Integer = {12, 24, 36, 48}
Dim names() As String = {"K", "S", "S", "D", "N"}
Dim miscData() As Object = {"this", 12d, 16ui, "a"c}
Dim objArray() As Object
miscData = names
Dim myArray1a() As Integer " Uninitialized array
Dim myArray1b As Integer() " Uninitialized array
Dim myArray2(10) As Integer " 1-dimensional array with 11 elements
Dim Employees1( , ) As Object " Uninitialized 2-D array
Dim Employees(200, 2) As Object " 2-D array with 201 and 3 elements
Dim jagged1(9)() As String
Dim jagged2()() As String = New String(9)() {}
Dim myArray1arr As Array
Dim myArray2a As Array = Array.CreateInstance(GetType(Integer), 10)
Dim members() As Integer = {3, 10}
Dim myArray3a As Array = Array.CreateInstance(GetType(Integer), members)
End Sub
End Module
Array.GetLowerBound
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
Array.GetUpperBound
Module Tester
Sub Main()
Dim i, j As Integer
" create rectangular two-dimensional array
Dim array1 As Integer(,)
array1 = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
" create jagged two-dimensional array
Dim array2 As Integer()() = New Integer(2)() {}
array2(0) = New Integer() {1, 2}
array2(1) = New Integer() {3}
array2(2) = New Integer() {4, 5, 6}
For i = 0 To array1.GetUpperBound(0)
For j = 0 To array1.GetUpperBound(1)
Console.Write(array1(i, j) & " ")
Next
Console.WriteLine("")
Next
For i = 0 To array2.GetUpperBound(0)
For j = 0 To array2(i).GetUpperBound(0)
Console.WriteLine(array2(i)(j) & " ")
Next
Console.WriteLine("")
Next
End Sub
End Module
Array.GetValue
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
Array.IndexOf
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.LastIndexOf
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
Array.Length
Module Tester
Sub Main()
Dim i As Integer
Dim array As Integer() " declare array variable
array = New Integer(9) {} " allocate memory for array
Console.WriteLine("Subscript " & vbTab & "Value")
For i = 0 To array.GetUpperBound(0)
Console.WriteLine( i & vbTab & array(i))
Next
Console.WriteLine("The array contains " & _
array.Length & " elements.")
End Sub
End Module
Array.Reverse
Imports System
Public Class MainClass
Shared Sub Main()
Dim myArray As [String]( ) = {"W", "i", "A", "t"}
Console.WriteLine("Display myArray...")
DisplayArray(myArray)
Console.WriteLine("Reverse and display myArray...")
Array.Reverse(myArray)
DisplayArray(myArray)
Dim myOtherArray As [String]( ) = _
{"e", "l", "s", "T", "o", "B", "f", "v"}
Console.WriteLine("Display myOtherArray...")
DisplayArray(myOtherArray)
Console.WriteLine("Sort and display myOtherArray...")
Array.Sort(myOtherArray)
DisplayArray(myOtherArray)
End Sub
Public Shared Sub DisplayArray(ByVal theArray( ) As Object)
Dim obj As Object
For Each obj In theArray
Console.WriteLine("Value: {0}", obj)
Next obj
Console.WriteLine(ControlChars.Lf)
End Sub "DisplayArray
End Class
Array.SetValue
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
Array.Sort
Option Strict On
Imports System.Collections
Public Module modMain
Public Sub Main()
Dim airports As New Hashtable
airports.Add("S", "SSS")
airports.Add("X", "XXX")
airports.Add("D", "DDD")
airports.Add("R", "RRR")
Dim values As ICollection = airports.Values
Dim valuesArray(airports.Count - 1) As String
values.CopyTo(valuesArray, 0)
Array.Sort(valuesArray)
Console.WriteLine()
For Each value As String in valuesArray
Console.WriteLine(value)
Next
End Sub
End Module