arrays vb.net 2010. declaring arrays dim salary(15) as integer ‘ 16 elements salary(15) = 10000...
TRANSCRIPT
ArraysArraysVB .NET 2010
Declaring ArraysDeclaring ArraysDim Salary(15) As Integer ‘ 16 Elements
Salary(15) = 10000
for each s as Integer in SalaryConsole.WriteLine( s.toString( “Rs #.00”) )
next
Dim Names( ) As String = {“Joe”, “Peter “}
SORTING ARRAYSSORTING ARRAYS
System.array.Sort( arrayName )
System.array.Sort( arrayName,
startIndex, endIndex )
System.array.Sort( array1, array2
)
SEARCHING ARRAYSSEARCHING ARRAYS i = System.array.IndexOf( arrayName, Object )
i = System.array.IndexOf( arrayName, Object ,
startIndex)
i = System.array.IndexOf( arrayName, Object ,
startIndex, endIndex )
i = System.array.BinarySearch( arrayName, Object )
i = System.array.BinarySearch( arrayName,
startIndex, searchLength, Object )
-i – 1 ‘ one’s complement
ARRAYS . . .ARRAYS . . .
newArraay = System.array.Reverse( array1)
System.array.copy( array1, array2, count )
System.array.copyto(destinationArray,
sourceStart)
◦Both Copy and Copyto are one dimensional only
System.array.copy( sourceArray,
sourceStart, destinationArray,
destinationStart, count )
StructuresStructures
Structure Employee
Dim Name As String
Dim Salary As Decimal
Dim DOB as Date
End Structure
Dim E1 as New EmployeeE1.DOB = #01/31/2000#
Multidimensional Multidimensional ArraysArrays
Temperatures(2, 0) ‘ is the third city’s nameTemperatures(2, 1) ‘ is the third city’s temperature
Multidimensional Multidimensional Arrays . . .Arrays . . .Dim a(,) As Integer = {{10, 20, 30},
{11, 21, 31}, {12, 22, 32}}
Console.WriteLine(a(0, 1)) ’ will print 20
Console.WriteLine(a(2, 2)) ’ will print 32
To find out the number of dimensions :a.Rank ’ will print 2
Multidimensional Multidimensional Arrays . . .Arrays . . .
Console.WriteLine(a.GetLength(1)) ’ # of elements in second dimension = 3
Console.WriteLine(a.Length) ’ total elements = 9
Console.WriteLine(a.GetUpperBound(0))’ last index in the first dimension = 2
Console.WriteLine(a.GetLowerBound(1))’ first index in the second dimension = 3
LISTLIST
Dim names As New List(Of String)
Dim colors As New List(Of Color)
names.Add(“Richard”)
names.Add(“Nancy”)
colors.Add(Color.Red)
colors.Add(TextBox1.BackColor)
If names.Contains(name) Then
MsgBox("Duplicate ")
Iterating Through a ListIterating Through a ListFor i = 0 To Lst.Count - 1
{ process item Lst(i)}Next
For Each itm As String In Lst’ process item lst
Next
Dim itm As ObjectFor Each itm In aLst
{ process item itm }Next
DictionaryDictionary
Dim BDays As New Dictionary(Of String,
Date)
BDays.Add(“Manfred”, #3/24/1972#) ‘
Key & Value
BDays.Add(“Alfred”, #11/24/1959#)
To retrieve the birth date of Manfred,
use the following statement:
BDays(“Manfred”)
Dictionary . . Dictionary . .
Dictionary.ContainsKey(object)
Dictionary.ContainsValue(object)
Dim itm As Object
For Each itm In Dict.Values
Debug.WriteLine(itm)
Next
HashTableHashTable
Dim tmps As New Hashtable
tmps("Houston") = 81.3
tmps("Los Angeles") = 78.5
ArrayListArrayList
Dim aList As New ArrayList
‘ Don’t need to define the type as in
LIST
aList.capacity = 100
aList.Insert(index, object)
Lst.AddRange(colors)
Lst.InsertRange(index, objects)
Lst.SetRange(5, words) ‘overwrite
ArrayList . . .ArrayList . . .
aList.Remove(object)
Lst.RemoveRange(startIndex, count)
newLst = Lst.GetRange(index, count)
newList = ArrayList.Repeat(item,
count)
newList = aList.Repeat(item, count)
newList =
System.arrayList.Repeat(item, count)
ArrayList . . .ArrayList . . .
newList = aList.Revese()
newList = aList.Revese(startIndex
, endIndex)
Sorting and Searching are same
as in Arrays
Collection InitializersCollection InitializersDim letters As New ArrayList ( {"quick", "dog",
"fox", "lazy", "brown"})
Dim words = New List(Of String) ( {"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog“ } )
Dim numbers() As Decimal = {2.0, 12.99, 0.001, 1000.0, 10.01}
Dim P = New With {.First = "Evangelos", .Last = "Petroutsos"} ‘P is of an anonymous type
Sorting ListsSorting Lists
aLst.Sort()
aLst.Sort(comparer)
aLst.Sort(startIndex, endIndex,
comparer)
SortedListSortedListDim sList As New SortedListDim sList As New SortedList(New
comparer)sList.Add(key, item)
Searching ListsSearching ListsLst.IndexOf(object)
Lst.IndexOf(object, startIndex)
Lst.IndexOf(object, startIndex, length)
Dim index As Integer = Lst.BinarySearch(object)
Dim index As Integer = Lst.BinarySearch(object,
comparer)
Dim index As Integer = Lst.BinarySearch (startIndex,
length, object, comparer)
Implementing the IComparer Implementing the IComparer InterfaceInterface
Class customComparer : Implements IComparer
Public Function Compare(
ByVal o1 As Object, ByVal o2 As Object)
As Integer Implements IComparer.Compare
{ function’s code
Return -1, 0 and 1
}
End Function
End Class
Implementing the IComparer Implementing the IComparer Interface . . .Interface . . .
Dim CompareThem As New customComparer
aList.Sort(CompareThem)
aList.Sort(New customComparer)
aList.BinarySearch(object, New
customComparer)
File HandlingFile HandlingDim str As StreamReader = File.OpenText(path)
Dim txtLine As String
Dim words() As String
Dim Delimiters() As Char = { CType(" ", Char),
CType(".", Char), CType(",", Char), CType("?",
Char), CType("!", Char), CType(";", Char),
CType(":", Char), Chr(10), Chr(13), vbTab }
txtLine = str.ReadToEnd
words = txtLine.Split(Delimiters)