1 / 23

שימוש במערך דינמי: ArrayList

שימוש במערך דינמי: ArrayList. מאפיינים חשובים בכל LIST. יכולת להכניס מידע בלי תלות בטיפוס יכולת למחו ק מידע יכולת להוסיף מידע פונקציות נוספות (מיון, חיפוש וכו'). שימוש בסיסי ב ARRAYLIST. Module Module1 Sub Main() Dim ItemList As New ArrayList () 'No datatype OR length!

keitha
Télécharger la présentation

שימוש במערך דינמי: ArrayList

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. שימוש במערך דינמי:ArrayList

  2. מאפיינים חשובים בכל LIST • יכולת להכניס מידע בלי תלות בטיפוס • יכולת למחוק מידע • יכולת להוסיף מידע • פונקציות נוספות (מיון, חיפוש וכו')

  3. שימוש בסיסי ב ARRAYLIST Module Module1 Sub Main() Dim ItemList As New ArrayList() 'No datatype OR length! ItemList.Add("Item4") ItemList.Add("Item5") ItemList.Add("Item2") ItemList.Add("Item1") ItemList.Add("Item3") ItemList.Insert(3, "Item6") 'insert after position 2 (into pos 3) ItemList.Sort() 'sort items in an arraylist ItemList.Reverse() ' reverses ItemList.Remove("Item1") 'remove based on value ItemList.RemoveAt(2) 'removes based on position Console.WriteLine("Where is Item5? " & ItemList.IndexOf("Item5")) End Sub End Module

  4. נוסיף פונקציית הדפסה Sub Print(ByVal x As ArrayList) Dim i As Integer Console.WriteLine("Shows Added Items") For i = 0 To x.Count() - 1 Console.WriteLine(x(i)) Next End Sub

  5. שימוש בסיסי ב ARRAYLIST, והדפסת התוכן SubMain() DimItemListAsNewArrayList() 'No datatype OR length! ItemList.Add("Item4") ItemList.Add("Item5") ItemList.Add("Item2") ItemList.Add("Item1") ItemList.Add("Item3") Print(ItemList) ItemList.Insert(3, "Item6") 'insert after pos 2(into pos 3) Print(ItemList) ItemList.Sort() 'sort items in an arraylist Print(ItemList) ItemList.Reverse() ' reverses Print(ItemList) ItemList.Remove("Item1") 'remove based on value Print(ItemList) ItemList.RemoveAt(2) 'removes based on position Print(ItemList) Console.WriteLine("Where is Item5? " & ItemList.IndexOf("Item5")) EndSub

  6. הפלט Shows Added Items Item4 Item5 Item2 Item1 Item3 Shows Added Items Item4 Item5 Item2 Item6 Item1 Item3 Shows Added Items Item1 Item2 Item3 Item4 Item5 Item6 ---------------------------- Shows Added Items Item6 Item5 Item4 Item3 Item2 Item1 Shows Added Items Item6 Item5 Item4 Item3 Item2 Shows Added Items Item6 Item5 Item3 Item2 Where is Item5? 1

  7. ArrayList מאפשר הגדרת מערך בלי סוג או אורך! SubMain() DimItemListAsNewArrayList() 'No datatype OR length! ItemList.Add("Item4") ItemList.Add("Item5") ItemList.Add("Item2") ItemList.Add("Item1") ItemList.Add("Item3") Print(ItemList) ItemList.Insert(3, "Item6") 'insert after pos 2(into pos 3) Print(ItemList) ItemList.Sort() 'sort items in an arraylist Print(ItemList) ItemList.Reverse() ' reverses Print(ItemList) ItemList.Remove("Item1") 'remove based on value Print(ItemList) ItemList.RemoveAt(2) 'removes based on position Print(ItemList) Console.WriteLine("Where is Item5? " & ItemList.IndexOf("Item5")) EndSub

  8. ניזכר ב structure StructureOved Dim name AsString DimmaskoretAsInteger EndStructure SubPrintOved(ByValx AsArrayList) DimiAsInteger Console.WriteLine("Shows Added Items") Fori = 0 Tox.Count() - 1 Console.WriteLine(x(i).name) Console.WriteLine(x(i).maskoret) Next EndSub

  9. שימוש בSTRUCTURE עם ARRAYLIST ModuleModule1 Sub Main() Dim array1 AsNewArrayList() 'No datatype OR length! DimansAsString = "yes" Dim item AsOved Whileans = "yes" Console.WriteLine("Enter name, maskoret, and if again (yes):") item.name = Console.ReadLine() item.maskoret = Console.ReadLine() array1.Add(item) ans = Console.ReadLine() EndWhile PrintOved(array1) EndSub EndModule

  10. שימוש בINSERT Module Module1 Sub Main() Dim ItemList As New ArrayList() 'No datatype OR length! ItemList.Add(15) ItemList.Add(5) ItemList.Add(13) ItemList.Insert(1, 6) 'insert an item after position 0 (into pos 1) Print(ItemList) End Sub Sub Print(ByVal x As ArrayList) Dim i As Integer Console.WriteLine("Shows ALL Items") For i = 0 To x.Count() - 1 Console.WriteLine(x(i)) Next End Sub End Module

  11. מוסג של ByRef- אין RETURN! Module Module1 Sub Swap(ByRef x As Integer, ByRef y As Integer) Dim temp As Integer = x x = y y = temp End Sub Sub Main() Dim a As Integer = 3, b As Integer = 4 Console.WriteLine("A is " & a & " b is " & b) Swap(a, b) 'No return!!! Console.WriteLine("A is " & a & " b is " & b) End Sub End Module

  12. השוואה למערך - שימוש במערך במקום ArrayList Module Module1 Sub Main() Dim ItemList(0) As Integer 'With datatype AND length! Console.WriteLine(ItemList.Length()) ItemList(0) = 15 Array.Resize(ItemList, ItemList.Length() + 1) ItemList(1) = 5 Array.Resize(ItemList, ItemList.Length() + 1) ItemList(2) = 13 PrintArr(ItemList) InsertToArr(ItemList, 1, 6) PrintArr(ItemList) End Sub המשך.....

  13. השוואה למערך- שינויים והוספות בפונקציות המשך ... Sub PrintArr(ByVal x() As Integer) Dim i As Integer Console.WriteLine("Shows Added Items") For i = 0 To x.Count() - 1 Console.WriteLine(x(i)) Next End Sub Sub InsertToArr(ByRef x() As Integer, ByVal place As Integer, ByVal 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

  14. Collectionsמבני נתונים לאוסף נתונים • Array List • Sorted List • Hash Table • Stack • Queue • These can be accessed via the System.Collections namespace

  15. Stack - מחסנית • מבנה נתונים שבו הנתונים נשלפים בסדר הפוך לסדר שבו הוכנסו • מממש אלגוריתם של • LIFO (Last In First Out) • הוספת איבר ע"י פונקציה בשם • Push() • שליפת איבר ע"י פונקציה בשם • Push()

  16. Queue - תור • מבנה נתונים שבו הנתונים נשלפים באותו סדר שבו הוכנסו • מממש אלגוריתם של • FIFO (First In First Out) • הוספת איבר ע"י פונקציה בשם • Enqueue() • שליפת איבר ע"י פונקציה בשם • Dequeue()

  17. ArrayList, Stack, Queueמה יותר טוב או מהר?! • הArrayListהוא אובייקט יותר מתקדם • פונקציות חדשות • ArrayListמבוסס על רשימה מקושרת • יש גם וריאציות שונות של STACK / QUEUE • נלמד בהמשך

  18. Array List • ArrayList הוא מערך דינמי • כאשר מוסיפים אלמנט הוא גדל • חיפוש מבוסס על האינדקס • חיפוש סדרתי מבוסס על איברים • לכן מאד איטי • חסכוני במקום

  19. Sorted List • איברים נשמרים כזוג של • מפתח וערך (key value pairs) • המיון היא לפי המפתח • חיפוש אפשרי לפי האינדקס או המפתח • מהיר יותר מ ArrayList • תפיסת מקום בינונית • אסור שהמפתח יהיה כפול או ריק • ערכים יכולים להיות כפולים או ריקים

  20. Hash Table • איברים נשמרים כזוג של • מפתח וערך (key value pairs) • השמירה היא לפי המפתח • חיפוש מבוסס על המפתח • מהיר מאד • תפיסת המון מקום • מותר שהמפתח יהיה כפול או ריק

  21. איך עושים את זה בLINK? Public Class Link Private m_MyData As String Private ID As Integer Private m_NextLink As Link Public Sub New(ByVal myParent As Link, ByVal theData As String, ByVal theID As Integer) m_MyData = theData ID = theID myParent.m_NextLink = Me End Sub Public Sub New(ByVal theData As String, ByVal theID As Integer) m_MyData = theData ID = theID End Sub Function MyData() As String Return m_MyData End Function Function MyID() As Integer Return ID End Function Function NextLink() As Link Return m_NextLink End Function End Class

  22. אבל אז יש צורך לשנות... Public Class LinkedList Private m_CurrentLink As Link Private m_FirstLink As Link Private Size As Integer Public Sub New(ByVal theData As String, ByVal theID As Integer) m_CurrentLink = New Link(theData, theID) m_FirstLink = m_CurrentLink Size = 1 End Sub Public Function MakeLink(ByVal currentLink As Link, ByVal x As String, ByVal theID As Integer) As Link m_CurrentLink = New Link(currentLink, x, theID) Size = Size + 1 Return m_CurrentLink End Function Function GetNextLink(ByVal aLink As Link) As Link Return aLink.NextLink() End Function Function GetCurrentLink() As Link Return m_CurrentLink End Function Function GetFirstLink() As Link Return m_FirstLink End Function

  23. וגם הMAIN... Module Module1 Sub Main() Dim List As New LinkedList("Avi ", 1) Dim aLink As Link = List.GetCurrentLink aLink = List.MakeLink(aLink, "Bob ", 3) aLink = List.MakeLink(aLink, "Chaim ", -1) aLink = List.MakeLink(aLink, "Dovid ", -5) List.PrintAll() End Sub End Module

More Related