Web Services אוספים - Collections ליווי מקצועי : ארז קלר

Slides:



Advertisements
Similar presentations
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Advertisements

Generics, Lists, Interfaces
Fun with Lists for the Novice and Expert Scott Reed Brain Hz Software (760)
Data Structures and Collections
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
1 Generic Collections Chapter Objectives You will be able to Use generic collection classes available in the.NET framework.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Arrays, Lists, Stacks, Queues Static and Dynamic Implementation Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Windows Database Applications CIS 341 Chapter 11.
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Arrays and Collections Tonga Institute of Higher Education.
C# Collections & Generics C#.NET Software Development Version 1.0.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
C# Collections & Generics
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
8. Hafta İçeriği ArrayList TheArrayList class supports dynamic arrays, which can grow or shrink as needed. In C#, standard arrays are of a fixed length,
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Week 15 – Monday.  What did we talk about last time?  Tries.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Lecture 10 Collections Richard Gesick.
Sort & Search Algorithms
Elementary Data Structures
Advanced .NET Programming I 2nd Lecture
Advanced Data Collections
Computing with C# and the .NET Framework
Data Structures Lakshmish Ramaswamy.
QueueStack CS1020.
Inheritance & Polymorphism
Chapter 5: Programming with C#
Week 15 – Monday CS221.
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Data Structures and Database Applications Queues in C#
Implementing Polymorphism
CS313D: Advanced Programming Language
CS1S467 GUI Programming LECTURE 11 Collections
Lecture 21 Stacks and Queues Richard Gesick.
Data Structures and Database Applications Arrays And Lists
COMPUTER 2430 Object Oriented Programming and Data Structures I
More Data Structures (Part 1)
Fundaments of Game Design
Recall: stacks and queues
鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
Defining Interfaces using C#
Lecture 16 Stacks and Queues CSE /26/2018.
Advanced .NET Programming I 3rd Lecture
Presentation transcript:

Web Services אוספים - Collections ליווי מקצועי : ארז קלר www.corner.co.il

מערכים מול אוספים. המחלקה ArrayList המחלקהHashtable . המחלקה Queue. המחלקה Stack. אוספים מותאמים אישית.

מערכים מול אוספים השוואה מערכים הם בגודל קבוע, גודלם נקבע בזמן הקצאתם, ולא ניתן לשנותם. אוספים הם בגודל משתנה, בזמן הקצאתם מוגדר להם גודל התחלתי, בעת הצורך הם גדלים או קטנים . האלמנטים במערך אינם יכולים להיות Read Only . באוספים כן. מערכים לא חייבים להיות הטרוגניים. אוספים הם תמיד הטרוגניים. מערכים הם מהירים יותר אולם פחות גמישים. אוספים הם גמישים יותר אולם איטיים יותר.

מערכים מול אוספים. המחלקה ArrayList. המחלקהHashtable . המחלקה Queue. המחלקה Stack. אוספים מותאמים אישית.

המחלקה ArrayList מחלקת ה- Collection השכיחה והפופולרית ביותר. מייצגת אוסף הטרוגני של אובייקטים, מגדירה מערך פנימי של ייחוסים מטיפוס System.Object . המחלקה מגדילה את עצמה אוטומטית בעת הצורך, כברירת מחדל גודלה הראשוני הוא 16 מקומות, כאשר נוצר הצורך המערך גדל ב- 16 מקומות נוספים, ערכים אלו ניתנים לשינוי. מוגדרת במרחב השמות System.Collection . ניתן לרשת אותה על מנת ליצור מחלקת אוסף מתמחה (וזה אפילו מומלץ).

המחלקה ArrayList המאפיינים המרכזיים של ArrayList מאפיין תיאור Capasity מכיל את גודלו הפיזי של המערך, מספר האלמנטים המקסימאלי שהוא יכול להכיל ללא הגדלתו, ברירת המחדל ברגע הקצאתו היא 16 . Count מספר האלמנטים הקיימים בפועל במערך, חייב להיות קטן או שווה ל- Capasity . IsFixedSize מאפיין בוליאני הבודק האם האובייקט מוגדר כבעל גודל קבוע, לא ניתן לשנות את גודלו של אובייקט כזה, אם כי ניתן לשנות את ערכי האינדקסים. IsReadOnly מאפיין בוליאני הבודק האם האובייקט מוגדר לקריאה בלבד, לא ניתן לשנות את גודלו או את ערכיו.

המחלקה ArrayList המתודות המרכזיות של ArrayList מתודה תיאור Add מתודה וירטואלית המוסיפה אובייקט בודד לסוף המערך, מחזירה את האינדקס. AddRange מתודה וירטואלית המוסיפה מספר איברים בקריאה אחת, מצרפת מערך למערך הנוכחי. BinarySearch מתודה המממשת חיפוש בינארי ("ארי במדבר"), מתודה זו מועמסת מספר פעמים במחלקה. Clear מתודה וירטואלית המנקה את המערך מכל האלמנטים המוצבים בו, Count מתאפס, Capasity נשאר ללא שינוי. Contains מתודה בוליאנית הבודקת האם אובייקט נמצא במערך. חיפוש האובייקט הוא ליניארי, לכן במקרים רבים בהם המערך ממוין נעדיף להשתמש ב-BinarySearch(...)

המחלקה ArrayList המתודות המרכזיות של ArrayList - המשך מתודה וירטואלית זו מחפשת האם האובייקט המתקבל כפרמטר נמצא במערך, מחזירה את האינדקס שלו במידה ונמצא, או -1 במידה והא אינו קיים במערך. IndexOf מתודה וירטואלית המוסיפה אלמנט חדש למערך במיקום מוגדר, שאר האלמנטים נדחפים למטה. Insert מתודה וירטואלית המחפשת ומסירה את המופע הראשון של האובייקט אשר נשלח כפרמטר, החיפוש הוא ליניארי. Remove מתודה וירטואלית המסירה מהמערך את האלמנט שמיקומו נשלח כפרמטר. RemoveAt מתודה וירטואלית ההופכת את הסדר של האלמנטים במערך. Reverse מתודה וירטואלית הממיינת את האלמנטים במערך. Sort מתאימה את גודלו של המערך למספר האלמנטים הקיימים בו בפועל, משפיעה על המאפיין Capacity . TrimToSize

המחלקה ArrayList דוגמה: ArrayList arr = new ArrayList(); arr.Add("Moshe"); arr.Add("David"); arr.Add("Yoram"); arr.Add("Yosi"); arr.Add("Dror"); foreach(string s in arr) Console.WriteLine(s); arr.Sort(); Console.WriteLine("Yoram positioned : {0}", arr.IndexOf("Yoram")); הוספת אובייקט לאוסף. הדפסת תוכן האוסף. מיון איברי האוסף. קבלת מיקומו של איבר באוסף.

המחלקה ArrayList דוגמה- המשך: דוגמה- המשך: Console.WriteLine("Is Eran in my list? - {0}",arr.Contains("Eran")); arr.Insert(3,"Danny"); foreach(string s in arr) Console.WriteLine(s);. Console.WriteLine("count : {0}",arr.Count); Console.WriteLine("Size : {0}",arr.Capacity); arr.TrimToSize(); הוספת אובייקט חדש במיקום מוגדר. בדיקת גודל פיזי וגודל לוגי. התאמת גודל המערך למספר האלמנטים

מערכים מול אוספים. המחלקה ArrayList. המחלקהHashtable . המחלקה Queue. המחלקה Stack. אוספים מותאמים אישית.

המחלקה Hashtable מחלקת אוסף שכיחה נוספת, מייצגת אוסף הטרוגני של אובייקטים. האובייקטים מאורגנים באמצעות מפתח (Key) וערך (Value) . הגישה לאובייקט היא באמצעות המפתח אשר חייב להיות ייחודי. כל אובייקט המוצב במחלקת אוסף זו מאוחסן באובייקט מהמבנה DictionaryEntry המכיל שני מאפיינים חשובים: Object Key ו- Object Value . הן המפתח והן הערך יכולים להיות מכל טיפוס. המפתח אינו יכול להיות null , הערך יכול להיות null . גם מחלקה זו ניתנת לגזירה על מנת להתאימה לצרכים של התוכנית.

המחלקה Hashtable דוגמה: ה- Hashtable יכיל אוסף של אובייקטים ממחלקה זו. class Person { private readonly int m_ID; private string m_Name; public Person(int id, string name) { . . .} public int ID get { . . .} } public string Name ה- Hashtable יכיל אוסף של אובייקטים ממחלקה זו.

המחלקה Hashtable דוגמה - המשך: Hashtable hash = new Hashtable(); Person p = new Person(111,"Shoshana"); hash.Add(p.ID,p); p = new Person(112,"Yerachmiel"); hash.Add(p.ID,p);. p = new Person(113,"Zalman"); p = (Person)hash[111]; Console.WriteLine(p.Name); p = (Person)hash[112]; p = (Person)hash[113]; הוספת אובייקט ל- Hashtable. שליפת אובייקט מתוך Hashtable.

המחלקה Hashtable דוגמה - המשך: הדפסת המפתחות הדפסת הערכים foreach(int i in hash.Keys) Console.WriteLine(i); foreach(Object obj in hash.Values) Console.WriteLine(((Person)obj).Name); הדפסת המפתחות הדפסת הערכים

מערכים מול אוספים. המחלקה ArrayList. המחלקהHashtable . המחלקה Queue. המחלקה Stack. אוספים מותאמים אישית.

המחלקה Queue מייצגת אוסף אובייקטים הנשמר במבנה first-in, first-out collection. מגדירה מספר מתודות חשובות : Enqueue – הוספת אובייקט לסוף התור. Dequeue – הוצאת אובייקט מראש התורה. Peek – החזרת אובייקט מבלי להוציאו מראש התור. דוגמה: Queue q = new Queue(); q.Enqueue(12); q.Enqueue(13); q.Enqueue(14); q.Enqueue(15); q.Enqueue(16); q.Enqueue(17); הוספת ערכים לתור.

המחלקה Queue דוגמה - המשך int num; num = (int)q.Dequeue(); Console.WriteLine(num); שליפת אובייקטים מהתור.

מערכים מול אוספים. המחלקה ArrayList. המחלקהHashtable . המחלקה Queue. המחלקה Stack. אוספים מותאמים אישית.

המחלקה Stack מגדירה מספר מתודות חשובות : דוגמה: Stack s = new Stack(); מייצגת אוסף אובייקטים הנשמר במבנה Last-in, first-out collection. מגדירה מספר מתודות חשובות : Push – הוספת אובייקט לראש המחסנית. Pop – הוצאת אובייקט מראש המחסנית. Peek – החזרת אובייקט מבלי להוציאו מראש המחסנית. דוגמה: Stack s = new Stack(); s.Push(12); s.Push(13); s.Push(14); s.Push(15); s.Push(16); s.Push(17); הוספת אובייקטים למחסנית.

שליפת אובייקטים מהמחסנית. המחלקה Stack דוגמה – המשך int num; num = (int)s.Pop(); Console.WriteLine(num); שליפת אובייקטים מהמחסנית.

מערכים מול אוספים. המחלקה ArrayList. המחלקהHashtable . המחלקה Queue. המחלקה Stack. אוספים מותאמים אישית.

אוספים מותאמים אישית הטיפול וההתנהגויות של מחלקה המטפלת באובייקט יחיד שונה לחלוטין מהטיפול וההתנהגויות הנדרשות מאוסף אובייקטים. בדרך כלל כתיבת מחלקה המטפלת ביחיד מחייבת כתיבת מחלקה המטפלת באוסף אובייקטים. לדוגמה : מהמחלקה Circle ניתן להקצות אובייקטים המטפלים באובייקט עיגול המצוייר על המסך, ההתנהגויות יכולות להיות : איתחול האובייקט, שינוי מיקום, שינוי גודל, שינוי צבע, חישוב שטח וכו'. אם נרצה לצייר על המסך מספר עיגולים, נגדיר מחלקה שתטפל באוסף עיגולים (CircleArr), ההתנהגויות יכולות להיות : הוסף אובייקט עיגול, הסר אובייקט עיגול, צייר את כולם, חפש אובייקט על פי רדיוס או על פי צבע וכו'.

אוספים מותאמים אישית ניתן לייצר מחלקת אוסף מותאמת בשני דרכים: כתיבת מחלקה המכילה מחלקת אוסף. כתיבת מחלקה היורשת מחלקת אוסף. הכלה class CircleArr { private ArrayList m_Arr; . . . } class CircleArr : ArrayList { . . . } הורשה

אוספים מותאמים אישית דוגמה - מחלקת אוסף ביחסי הכלה: נתונה המחלקה Circle המטפלת באובייקט עיגול יחיד: class Circle { private Point m_Location; private int m_Radius; public Circle(int x, int y, int r) {. . .} public void SetLocation(int x, int y) { . . .} public void SetSize(int r) {. . .} public void Print() {. . .} }

אוספים מותאמים אישית דוגמה - מחלקת אוסף ביחסי הכלה: class CircleArr { private ArrayList m_Arr; public CircleArr() m_Arr = new ArrayList(); } public void Add(Circle c) m_Arr.Add(c);

אוספים מותאמים אישית דוגמה - מחלקת אוסף ביחסי הכלה - המשך: public void Remove(Circle c) { m_Arr.Remove(c); } public void Print() foreach (Circle c in m_Arr) c.Print();

אוספים מותאמים אישית דוגמה - מחלקת אוסף ביחסי הורשה class CircleArr:ArrayList { public override int Add(object value) if (!(value is Circle)) return -1; return base.Add(value); } public override void AddRange(ICollection c) foreach(Object obj in c) if (!(obj is Circle)) return; base.AddRange(c); public void Print() foreach (Circle c in this) c.Print();

אוספים - Collections עריכה מקצועית : ארז קלר. www.corner.co.il מערכים מול אוספים. המחלקה ArrayList המחלקהHashtable . המחלקה Queue. המחלקה Stack. אוספים מותאמים אישית. עריכה מקצועית : ארז קלר. www.corner.co.il