Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays VB.NET 2010. Declaring Arrays Dim Salary(15) As Integer ‘ 16 Elements Salary(15) = 10000 for each s as Integer in Salary Console.WriteLine( s.toString(

Similar presentations


Presentation on theme: "Arrays VB.NET 2010. Declaring Arrays Dim Salary(15) As Integer ‘ 16 Elements Salary(15) = 10000 for each s as Integer in Salary Console.WriteLine( s.toString("— Presentation transcript:

1 Arrays VB.NET 2010

2 Declaring Arrays Dim Salary(15) As Integer ‘ 16 Elements Salary(15) = 10000 for each s as Integer in Salary Console.WriteLine( s.toString( “Rs #.00”) ) next Dim Names( ) As String = {“Joe”, “Peter “}

3 SORTING ARRAYS System.array.Sort( arrayName ) System.array.Sort( arrayName, startIndex, endIndex ) System.array.Sort( array1, array2 )

4 SEARCHING 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

5 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 )

6 Structures Structure Employee Dim Name As String Dim Salary As Decimal Dim DOB as Date End Structure Dim E1 as New Employee E1.DOB = #01/31/2000#

7 Multidimensional Arrays Temperatures(2, 0) ‘ is the third city’s name Temperatures(2, 1) ‘ is the third city’s temperature

8 Multidimensional 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

9 Multidimensional 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

10 LIST 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 ")

11 Iterating Through a List For 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 Object For Each itm In aLst { process item itm } Next

12 Dictionary 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”)

13 Dictionary.. Dictionary.ContainsKey(object) Dictionary.ContainsValue(object) Dim itm As Object For Each itm In Dict.Values Debug.WriteLine(itm) Next

14 HashTable Dim tmps As New Hashtable tmps("Houston") = 81.3 tmps("Los Angeles") = 78.5

15 ArrayList 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

16 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)

17 ArrayList... newList = aList.Revese() newList = aList.Revese(startIndex, endIndex) Sorting and Searching are same as in Arrays

18 Collection Initializers Dim 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

19 Sorting Lists aLst.Sort() aLst.Sort(comparer) aLst.Sort(startIndex, endIndex, comparer)

20 SortedList Dim sList As New SortedList Dim sList As New SortedList(New comparer) sList.Add(key, item)

21 Searching Lists Lst.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)

22 Implementing the IComparer Interface 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

23 Implementing the IComparer Interface... Dim CompareThem As New customComparer aList.Sort(CompareThem) aList.Sort(New customComparer) aList.BinarySearch(object, New customComparer)

24 File Handling Dim 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)


Download ppt "Arrays VB.NET 2010. Declaring Arrays Dim Salary(15) As Integer ‘ 16 Elements Salary(15) = 10000 for each s as Integer in Salary Console.WriteLine( s.toString("

Similar presentations


Ads by Google