1 Generic Collections Chapter 18. 2 Objectives You will be able to Use generic collection classes available in the.NET framework.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 24 Lists, Stacks, and Queues
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Generics, Lists, Interfaces
Data Structures and Collections
Queues and Stacks.  Can receive multiple requests from multiple sources ◦ How do we services these requests?  First come, first serve processing 
CS Data Structures II Review COSC 2006 April 14, 2017
1 Derived Classes and Inheritance Chapter Objectives You will be able to: Create derived classes in C#. Understand polymorphism. Write polymorphic.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object Composition Interfaces Collections Covariance Object class Programming using C# LECTURE 10.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
ILM Proprietary and Confidential -
Chapter 18 Java Collections Framework
Murach’s Visual Basic 2008, modified, C8© 2008, Mike Murach & Associates, Inc. Slide 1.
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.
5. Collections Arrays Other basic data structures.NET collections Class library example.
Understanding Data Types and Collections Lesson 2.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Arrays and Collections Tonga Institute of Higher Education.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
Object Oriented Software Development 6. Arrays and collections.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
1 Introduction to Object Oriented Programming Chapter 10.
AUC Technologies Projects Consulting, Development, Mentoring, and Training Company Collections Presented By : Muhammad Atif Hussain Deputy Manager IT (Takaful.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Understanding Data Types and Collections Lesson 2.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Based on slides by Alyssa Harding & Marty Stepp
Fundaments of Game Design
Chengyu Sun California State University, Los Angeles
Presentation transcript:

1 Generic Collections Chapter 18

2 Objectives You will be able to Use generic collection classes available in the.NET framework

3 Generalized Collection Classes Array-like data structures (Chapter 10) ArrayList Queue Stack Hashtable SortedList Offer programming convenience for specific access needs. Store objects Add anything. Typecast on removal.

4 Generics Generics (Chapter 18) offer typesafe alternatives. New in.NET 2.0 (Visual Studio 2005) Generics are generally the preferred alternative. Object collection classes offer advantages in certain unusual cases.

5 Collection Classes To use these classes we must write using System.Collections.Generic; for generics Included in the default C# template using System.Collections; for object collections Not included in the default C# template

6 Generics C# generics are like C++ templates. Classes written with blanks to be filled in by the user (parameters) Cannot be instantiated directly. We create a specialized version for a specific type by supplying the name of the type when the class is used. Not a macro! Supplying the parameter effectively creates a new class, which can be instantiated.

7 The Generic List Class List is the generic List class T represents a class name parameter to be supplied in declarations. Provides traditional list operations Insert Delete Also provides array operations Access by position, using index notation

8 List Methods Add (T item) Add item at end of list Insert (int index, T item) Insert item at a specific position Remove (T item) Remove first occurance of item RemoveAt (int index) Remove item at specified position

9 List Methods Clear() Removes all items from the list bool Contains(T item) Determines if item is in the list int IndexOf(T item) Returns index of item, or -1 if not in list. Sort()... more

10 List Indexer Array index notation can be used to get or set a specified item. int_list[5] = 17; int temp = int_list[5]; Throws an exception if int_list[5] does not exist.

11 List Properties CountNumber of items in the list

12 List Example Create new C# Console Application project.

13 List Example static void Main(string[] args) { List ints = new List (); ints.Add(1); ints.Add(2); ints.Add(3); foreach (int i in ints) { Console.WriteLine(i.ToString() ); } Console.ReadLine(); }

14 List Example Running

A List of Circles Download Circles.cs from class web site Downloads area into project directory. Downloads/ Downloads/ Add Circles.cs to the project Delete namespaces. 15

16 List Example using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List circles = new List (); circles.Add(new Circle("C1", 1.0)); circles.Add(new Circle("C2", 2.0)); circles.Add(new Circle("c3", 3.0)); foreach (Circle c in circles) { Console.WriteLine(c.Name()); } Console.ReadLine(); }

17 List Example Running

18 Accessing List by Position static void Main(string[] args) { List circles = new List (); circles.Add(new Circle("C1", 1.0)); circles.Add(new Circle("C2", 2.0)); circles.Add(new Circle("c3", 3.0)); for (int i = 0; i < circles.Count; ++i) { Console.WriteLine(circles[i].Name()); } Console.ReadLine(); } Same result

19 Inserting static void Main(string[] args) { List circles = new List (); circles.Add(new Circle("C1", 1.0)); circles.Add(new Circle("C2", 2.0)); circles.Add(new Circle("c3", 3.0)); Circle c4 = new Circle("C4", 4.0); circles.Insert(2, c4); for (int i = 0; i < circles.Count; ++i) { Console.WriteLine(circles[i].Name()); } Console.ReadLine(); }

20 Inserting

21 Inserting and Deleting static void Main(string[] args) { List circles = new List (); circles.Add(new Circle("C1", 1.0)); circles.Add(new Circle("C2", 2.0)); circles.Add(new Circle("c3", 3.0)); Circle c4 = new Circle("C4", 4.0); circles.Insert(2, c4); circles.RemoveAt(1); for (int i = 0; i < circles.Count; ++i) { Console.WriteLine(circles[i].Name()); } Console.ReadLine(); }

22 Inserting and Deleting

Sorting List has a Sort method. Parameter class must implement the IComparable interface. Example: class Schedule_Record : IComparable { private String college;...

IComparable interface Class must implement CompareTo() method, taking an object of the same type as its parameter. Return negative int if this object < other Return 0 if this object == other return positive int if this object > other Same as strcmp() in C

Implementing IComparable class Circle : IComparable { private String name; private double radius = 0.0;... public int CompareTo(Circle other) { if (this.radius < other.radius) return -1; else if (this.radius > other.radius) return 1; else return 0; }

Using Sort()

Program Running End of Section

28 The Generic Queue Queue Methods Enqueue (T item) Add an item to the end of the queue T Dequeue() Removes and returns object at the head of the queue Clear(), Contains(), Peek(),... many more

29 Queue Example static void Main(string[] args) { Queue circles = new Queue (); circles.Enqueue(new Circle("C1", 1.0)); circles.Enqueue(new Circle("C2", 2.0)); circles.Enqueue(new Circle("c3", 3.0)); while (circles.Count > 0) { Circle c = circles.Dequeue(); Console.WriteLine(c.Name()); } Console.ReadLine(); }

30 Queue Example Running

31 Dictionary Stores (Key, Value) pairs Class KeyValuePair Template parameters K is type of Key V is type of Value

32 Dictionary Example static void Main(string[] args) { Dictionary circles = new Dictionary (); circles.Add("c1", new Circle("C1", 1.0)); circles.Add("c2", new Circle("C2", 2.0)); circles.Add("c3", new Circle("c3", 3.0)); foreach (KeyValuePair kvp in circles) { String k = kvp.Key; Circle c = kvp.Value; Console.WriteLine("Circle {0} has radius {1}", k, c.Radius()); } Console.ReadLine(); }

33 Dictionary Example

34 Using Key as Indexer static void Main(string[] args) { Dictionary circles = new Dictionary (); circles.Add("c1", new Circle("C1", 1.0)); circles.Add("c2", new Circle("C2", 2.0)); circles.Add("c3", new Circle("c3", 3.0)); Circle c = circles["c2"]; Console.WriteLine("Circle {0} has radius {1}", c.Name(), c.Radius()); Console.ReadLine(); }

35 Indexer Example Running

36 About Dictionary Key class must have a compare for equality operation. Keys must be unique. Attempting to Add an item with a key already in the Dictionary will result in an exception. Can set entry with an existing key, using indexer notation.

37 Adding with Existing Key static void Main(string[] args) { Dictionary circles = new Dictionary (); circles.Add("c1", new Circle("C1", 1.0)); circles.Add("c2", new Circle("C2", 2.0)); circles.Add("c3", new Circle("c3", 3.0)); Circle c = circles["c2"]; Console.WriteLine("Circle {0} has radius {1}", c.Name(), c.Radius()); circles["c2"] = new Circle("New C2", 200.0); c = circles["c2"]; Console.WriteLine("Circle {0} has radius {1}", c.Name(), c.Radius()); Console.ReadLine(); }

38 Adding with Existing Key

39 Other Generic Container Classes Linked List(No array operations) SortedDictionary SortedList Stack See.NET documentation

40 Summary The.NET Generics make life easier. Use List like an array without worrying about size. Plus additional features! Use Dictionary to store and retrieve objects by Key value. Use Stack and Queue when you need those concepts. End of Presentation