ARRAYLIST Module 1 Sub Main Dim Item List
ARRAYLIST שימוש בסיסי ב Module 1 Sub Main() Dim Item. List As New Array. List() 'No datatype OR length! Item. List. Add("Item 4") Item. List. Add("Item 5") Item. List. Add("Item 2") Item. List. Add("Item 1") Item. List. Add("Item 3") Item. List. Insert(3, "Item 6") 'insert after position 2 (into pos 3) Item. List. Sort() 'sort items in an arraylist Item. List. Reverse() ' reverses Item. List. Remove("Item 1") 'remove based on value Item. List. Remove. At(2) 'removes based on position Console. Write. Line("Where is Item 5? " & Item. List. Index. Of("Item 5")) End Sub End Module
נוסיף פונקציית הדפסה Sub Print(By. Val x As Array. List) Dim i As Integer Console. Write. Line("Shows Added Items") For i = 0 To x. Count() - 1 Console. Write. Line(x(i)) Next End Sub
והדפסת התוכן , ARRAYLIST שימוש בסיסי ב Sub Main() Dim Item. List As New Array. List() 'No datatype OR length! Item. List. Add("Item 4") Item. List. Add("Item 5") Item. List. Add("Item 2") Item. List. Add("Item 1") Item. List. Add("Item 3") Print(Item. List) Item. List. Insert(3, "Item 6") 'insert after pos 2(into pos 3) Print(Item. List) Item. List. Sort() 'sort items in an arraylist Print(Item. List) Item. List. Reverse() ' reverses Print(Item. List) Item. List. Remove("Item 1") 'remove based on value Print(Item. List) Item. List. Remove. At(2) 'removes based on position Print(Item. List) Console. Write. Line("Where is Item 5? " & Item. List. Index. Of("Item 5")) End Sub
הפלט Shows Added Items Item 4 Item 5 Item 2 Item 1 Item 3 Shows Added Items Item 4 Item 5 Item 2 Item 6 Item 1 Item 3 Shows Added Items Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 -------------- Shows Added Items Item 6 Item 5 Item 4 Item 3 Item 2 Item 1 Shows Added Items Item 6 Item 5 Item 4 Item 3 Item 2 Shows Added Items Item 6 Item 5 Item 3 Item 2 Where is Item 5? 1
! מאפשר הגדרת מערך בלי סוג או אורך Array. List Sub Main() Dim Item. List As New Array. List() 'No datatype OR length! Item. List. Add("Item 4") Item. List. Add("Item 5") Item. List. Add("Item 2") Item. List. Add("Item 1") Item. List. Add("Item 3") Print(Item. List) Item. List. Insert(3, "Item 6") 'insert after pos 2(into pos 3) Print(Item. List) Item. List. Sort() 'sort items in an arraylist Print(Item. List) Item. List. Reverse() ' reverses Print(Item. List) Item. List. Remove("Item 1") 'remove based on value Print(Item. List) Item. List. Remove. At(2) 'removes based on position Print(Item. List) Console. Write. Line("Where is Item 5? " & Item. List. Index. Of("Item 5")) End Sub
structure ניזכר ב Structure Oved Dim name As String Dim maskoret As Integer End Structure Sub Print. Oved(By. Val x As Array. List) Dim i As Integer Console. Write. Line("Shows Added Items") For i = 0 To x. Count() - 1 Console. Write. Line(x(i). name) Console. Write. Line(x(i). maskoret) Next End Sub
ARRAYLIST עם STRUCTURE שימוש ב Module 1 Sub Main() Dim array 1 As New Array. List() 'No datatype OR length! Dim ans As String = "yes" Dim item As Oved While ans = "yes" Console. Write. Line("Enter name, maskoret, and if again (yes): ") item. name = Console. Read. Line() item. maskoret = Console. Read. Line() array 1. Add(item) ans = Console. Read. Line() End While Print. Oved(array 1) End Sub End Module
INSERT שימוש ב Module 1 Sub Main() Dim Item. List As New Array. List() 'No datatype OR length! Item. List. Add(15) Item. List. Add(13) Item. List. Insert(1, 6) 'insert an item after position 0 (into pos 1) Print(Item. List) End Sub Print(By. Val x As Array. List) Dim i As Integer Console. Write. Line("Shows ALL Items") For i = 0 To x. Count() - 1 Console. Write. Line(x(i)) Next End Sub End Module
!RETURN אין -By. Ref מוסג של Module 1 Sub Swap(By. Ref x As Integer, By. Ref y As Integer) Dim temp As Integer = x x=y y = temp End Sub Main() Dim a As Integer = 3, b As Integer = 4 Console. Write. Line("A is " & a & " b is " & b) Swap(a, b) 'No return!!! Console. Write. Line("A is " & a & " b is " & b) End Sub End Module
Array. List שימוש במערך במקום - השוואה למערך Module 1 Sub Main() Dim Item. List(0) As Integer 'With datatype AND length! Console. Write. Line(Item. List. Length()) Item. List(0) = 15 Array. Resize(Item. List, Item. List. Length() + 1) Item. List(1) = 5 Array. Resize(Item. List, Item. List. Length() + 1) Item. List(2) = 13 Print. Arr(Item. List) Insert. To. Arr(Item. List, 1, 6) Print. Arr(Item. List) End Sub. . . המשך
שינויים והוספות בפונקציות - השוואה למערך . . . המשך Sub Print. Arr(By. Val x() As Integer) Dim i As Integer Console. Write. Line("Shows Added Items") For i = 0 To x. Count() - 1 Console. Write. Line(x(i)) Next End Sub Insert. To. Arr(By. Ref x() As Integer, By. Val place As Integer, By. Val value As Integer) Array. Resize(x, x. Length() + 1) Dim index As Integer For index = x. Length - 1 To place Step -1 x(index) = x(index - 1) Next x(place) = value End Sub End Module
Collections מבני נתונים לאוסף נתונים • • • Array List Sorted List Hash Table Stack Queue These can be accessed via the System. Collections namespace
? LINK איך עושים את זה ב Public Class Link Private m_My. Data As String Private ID As Integer Private m_Next. Link As Link Public Sub New(By. Val my. Parent As Link, By. Val the. Data As String, By. Val the. ID As Integer) m_My. Data = the. Data ID = the. ID my. Parent. m_Next. Link = Me End Sub Public Sub New(By. Val the. Data As String, By. Val the. ID As Integer) m_My. Data = the. Data ID = the. ID End Sub Function My. Data() As String Return m_My. Data End Function My. ID() As Integer Return ID End Function Next. Link() As Link Return m_Next. Link End Function End Class
. . . אבל אז יש צורך לשנות Public Class Linked. List Private m_Current. Link As Link Private m_First. Link As Link Private Size As Integer Public Sub New(By. Val the. Data As String, By. Val the. ID As Integer) m_Current. Link = New Link(the. Data, the. ID) m_First. Link = m_Current. Link Size = 1 End Sub Public Function Make. Link(By. Val current. Link As Link, By. Val x As String, By. Val the. ID As Integer) As Link m_Current. Link = New Link(current. Link, x, the. ID) Size = Size + 1 Return m_Current. Link End Function Get. Next. Link(By. Val a. Link As Link) As Link Return a. Link. Next. Link() End Function Get. Current. Link() As Link Return m_Current. Link End Function Get. First. Link() As Link Return m_First. Link End Function
. . . MAIN וגם ה Module 1 Sub Main() Dim List As New Linked. List("Avi ", 1) Dim a. Link As Link = List. Get. Current. Link a. Link = List. Make. Link(a. Link, "Bob ", 3) a. Link = List. Make. Link(a. Link, "Chaim ", -1) a. Link = List. Make. Link(a. Link, "Dovid ", -5) List. Print. All() End Sub End Module
- Slides: 23