שימוש במערך דינמי : arraylist. מאפיינים חשובים בכל list יכולת...

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

Post on 20-Dec-2015

234 views

Category:

Documents


7 download

TRANSCRIPT

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

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

יכולת להכניס מידע בלי תלות בטיפוס•

יכולת למחוק מידע•

יכולת להוסיף מידע•

פונקציות נוספות )מיון, חיפוש וכו'(•

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

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

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

, והדפסת התוכן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

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

Shows Added ItemsItem6Item5Item4Item3Item2Item1Shows Added ItemsItem6Item5Item4Item3Item2Shows Added ItemsItem6Item5Item3Item2Where is Item5? 1

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

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

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

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

!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

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

.....המשך

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

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

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

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

System.Collections namespace

Stackמחסנית -

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

מממש אלגוריתם של •

• LIFO (Last In First Out)הוספת איבר ע"י פונקציה בשם •

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

• Push()

Queueתור -

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

מממש אלגוריתם של •

• FIFO (First In First Out)הוספת איבר ע"י פונקציה בשם •

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

• Dequeue()

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

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

• ArrayListמבוסס על רשימה מקושרת

STACK / QUEUEיש גם וריאציות שונות של •נלמד בהמשך–

Array List

•ArrayList הוא מערך דינמי

כאשר מוסיפים אלמנט הוא גדל•

חיפוש מבוסס על האינדקס•

חיפוש סדרתי מבוסס על איברים•לכן מאד איטי–

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

Sorted List

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

המיון היא לפי המפתח•

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

תפיסת מקום בינונית•

אסור שהמפתח יהיה כפול או ריק•

ערכים יכולים להיות כפולים או ריקים•

Hash Table

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

השמירה היא לפי המפתח•

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

תפיסת המון מקום•

מותר שהמפתח יהיה כפול או ריק•

?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

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

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