23
ךךךךך ךךךךך :ךךךךךArrayList

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

  • Upload
    keitha

  • View
    41

  • Download
    2

Embed Size (px)

DESCRIPTION

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

Citation preview

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

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

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

LISTמאפיינים חשובים בכל

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

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

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 SubEnd Module

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

נוסיף פונקציית הדפסה

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

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

, והדפסת התוכןARRAYLISTשימוש בסיסי ב 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") 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"))End Sub

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

הפלטShows Added ItemsItem4Item5Item2Item1Item3Shows Added ItemsItem4Item5Item2Item6Item1Item3Shows Added ItemsItem1Item2Item3Item4Item5Item6----------------------------

Shows Added ItemsItem6Item5Item4Item3Item2Item1Shows Added ItemsItem6Item5Item4Item3Item2Shows Added ItemsItem6Item5Item3Item2Where is Item5? 1

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

ArrayList מאפשר הגדרת מערך בלי סוג או אורך!

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") 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"))End Sub

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

structureניזכר ב

Structure Oved Dim name As String Dim maskoret As Integer End Structure

Sub PrintOved(ByVal x As ArrayList) Dim i As Integer Console.WriteLine("Shows Added Items") For i = 0 To x.Count() - 1 Console.WriteLine(x(i).name) Console.WriteLine(x(i).maskoret) NextEnd Sub

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

ARRAYLIST עם STRUCTUREשימוש בModule Module1 Sub Main() Dim array1 As New ArrayList() 'No datatype OR length! Dim ans As String = "yes" Dim item As Oved While ans = "yes" Console.WriteLine("Enter name, maskoret, and if again (yes):") item.name = Console.ReadLine() item.maskoret = Console.ReadLine() array1.Add(item) ans = Console.ReadLine() End While PrintOved(array1) End SubEnd Module

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

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 SubEnd Module

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

!RETURN- אין ByRefמוסג של 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 SubEnd Module

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

- שימוש במערך במקום השוואה למערך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

.....המשך

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

- שינויים והוספות בפונקציות השוואה למערךהמשך ...

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 SubEnd Module

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

Collectionsמבני נתונים לאוסף נתונים

• Array List• Sorted List• Hash Table• Stack• Queue• These can be accessed via the

System.Collections namespace

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

Stackמחסנית -

מבנה נתונים שבו הנתונים נשלפים בסדר •הפוך לסדר שבו הוכנסו

מממש אלגוריתם של •• LIFO (Last In First Out)

הוספת איבר ע"י פונקציה בשם •• Push()

שליפת איבר ע"י פונקציה בשם •• Push()

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

Queueתור -

מבנה נתונים שבו הנתונים נשלפים באותו סדר •שבו הוכנסו

מממש אלגוריתם של •• FIFO (First In First Out)

הוספת איבר ע"י פונקציה בשם •• Enqueue()

שליפת איבר ע"י פונקציה בשם •• Dequeue()

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

ArrayList, Stack, Queue!?מה יותר טוב או מהר

הוא אובייקט יותר מתקדם ArrayList ה•פונקציות חדשות–

• ArrayListמבוסס על רשימה מקושרתSTACK / QUEUEיש גם וריאציות שונות של •

נלמד בהמשך–

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

Array List

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

לכן מאד איטי–

חסכוני במקום•

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

Sorted List

איברים נשמרים כזוג של•(key value pairsמפתח וערך )–

המיון היא לפי המפתח•חיפוש אפשרי לפי האינדקס או המפתח•

ArrayListמהיר יותר מ –תפיסת מקום בינונית•אסור שהמפתח יהיה כפול או ריק•ערכים יכולים להיות כפולים או ריקים•

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

Hash Table

איברים נשמרים כזוג של•(key value pairsמפתח וערך )–

השמירה היא לפי המפתח•חיפוש מבוסס על המפתח•

מהיר מאד–תפיסת המון מקום•מותר שהמפתח יהיה כפול או ריק•

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

?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 FunctionEnd Class

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

...אבל אז יש צורך לשנות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

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

...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 SubEnd Module