CS313D: Advanced Programming Language

Slides:



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

Generics, Lists, Interfaces
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
 2006 Pearson Education, Inc. All rights reserved Collections.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
 2006 Pearson Education, Inc. All rights reserved Collections Many slides modified by Prof. L. Lilien (even many without an explicit message indicating.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
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.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
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.
Understanding Data Types and Collections Lesson 2.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Understanding Data Types and Collections Lesson 2.
Sets and Maps Chapter 9.
Sections 10.5 – 10.6 Hashing.
Lecture 10 Collections Richard Gesick.
Sort & Search Algorithms
Advanced Data Collections
Computing with C# and the .NET Framework
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
12 C Data Structures.
Working with Collections of Data
Chapter 19 Java Data Structures
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Basic Data Structures.
Data Structures Interview / VIVA Questions and Answers
Cinda Heeren / Geoffrey Tien
Week 15 – Monday CS221.
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.
Searching.
Data Structures and Database Applications Hashing and Hashtables in C#
Top Ten Words that Almost Rhyme with “Peas”
Road Map CS Concepts Data Structures Java Language Java Collections
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
Arrays and Linked Lists
Data Structures and Database Applications Hashing and Hashtables in C#
Lecture 10 List Richard Gesick.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Can store many of the same kind of data together
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Can store many of the same kind of data together
Object Oriented Programming in java
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures (CS212D) Week # 2: Arrays.
Collections Framework
Can store many of the same kind of data together
Introduction to Data Structure
Sets and Maps Chapter 9.
Data Structures & Algorithms
Fundaments of Game Design
structures and their relationships." - Linus Torvalds
Lecture-Hashing.
Presentation transcript:

CS313D: Advanced Programming Language Computer Science department Lecture 8 : Collections

Lecture Contents Why collections? What is a collection? Non-generic collections: Array & ArrayList Stack HashTable dr. Amal Khalifa, Spr 17

Why collections? Data structures are essential to build apps, but there’s no need to build such them from scratch. Instead, you can use the prepackaged collection classes provided by the .NET Framework. System.Collections contains collections that store references to objects. dr. Amal Khalifa, Spr 17

What is a collection? Collection classes store collections of data Each instance of one of these classes is a collection of items. Examples: Arrays Stacks Queues HashTables dr. Amal Khalifa, Spr 17

interfaces of the .NET Framework collections Implementations of these interfaces are provided within the framework. dr. Amal Khalifa, Spr 17

interfaces of the .NET Framework collections GetEnumerator. Some collection types offer the GetEnumerator method. This method returns an Enumerator object that can be used to loop through the collection. Complete knowledge of the actual collection type is not needed. interfaces of the .NET Framework collections Implementations of these interfaces are provided within the framework. dr. Amal Khalifa, Spr 17

collection classes dr. Amal Khalifa, Spr 17

Class Array and Enumerators Use Array static methods to: Copy arrays Sort array elements Search array elements dr. Amal Khalifa, Spr 17

Line 22 uses static Array method Sort to sort array doubleValues. When this method returns, the array contains its original elements sorted in ascending order. Line 25 uses static Array method Copy to copy elements from array intValues to array intValuesCopy. dr. Amal Khalifa, Spr 17

Line 22 uses static Array method Sort to sort array doubleValues. When this method returns, the array contains its original elements sorted in ascending order. Line 22 uses static Array method Sort to sort array doubleValues. When this method returns, the array contains its original elements sorted in ascending order. Line 25 uses static Array method Copy to copy elements from array intValues to array intValuesCopy. dr. Amal Khalifa, Spr 17

Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Its behavior on an unsorted array is unpredictable dr. Amal Khalifa, Spr 17

Line 25 uses static Array method Copy to copy elements from array intValues to array intValuesCopy. Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Its behavior on an unsorted array is unpredictable dr. Amal Khalifa, Spr 17

BinarySearch assumes that it receives a sorted array. Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Its behavior on an unsorted array is unpredictable dr. Amal Khalifa, Spr 17

Enumerator!! GetEnumerator returns an enumerator that can iterate over the collection. dr. Amal Khalifa, Spr 17

Class Array and Enumerators Other static Array methods include Clear (to set a range of elements to 0, false or null, as appropriate) CreateInstance (to create a new array of a specified type) IndexOf (to locate the first occurrence of an object in an array or portion of an array) LastIndexOf (to locate the last occurrence of an object in an array or portion of an array) Reverse (to reverse the contents of an array or portion of an array). dr. Amal Khalifa, Spr 17

ArrayList dr. Amal Khalifa, Spr 17

Class ArrayList ArrayList collection class mimics the functionality of conventional arrays and provides dynamic resizing of the collection through the class’s methods. At any time, an ArrayList contains a certain number of elements less than or equal to its capacity—the number of elements currently reserved for the ArrayList. An app can manipulate the capacity with ArrayList property Capacity. dr. Amal Khalifa, Spr 17

dr. Amal Khalifa, Spr 17

Example Use an ArrayList to store string objects Use its methods to add, remove, search elements dr. Amal Khalifa, Spr 17

Code ArrayList constructor accepts a pre- allocated array!! dr. Amal Khalifa, Spr 17

Code Count vs. Capacity IndexOf returns the position of the element or a negative value if not found We use the indexer to obtain each of secondList’s elements, then remove each one from firstList with the Remove method. This method deletes a specified item from an ArrayList by performing a linear search and removing (only) the first occurrence of the specified object. All subsequent elements shift toward the beginning of the ArrayList to fill the emptied position. dr. Amal Khalifa, Spr 17

code Using Indexers Remove method applies a linear search and deletes only the first occurrence dr. Amal Khalifa, Spr 17

ArrayList & polymorphism 1 2 3 positions elements S l f dr. Amal Khalifa, Spr 17

Class Stack dr. Amal Khalifa, Spr 17

What is a stack? LIFO data structures dr. Amal Khalifa, Spr 17

Example Using directive Creating a stack of Object references Elements of different types  boxing dr. Amal Khalifa, Spr 17

Example Push adds an object on the top of the stack  grows to accommodate new elements Pop removes the object on the top of the stack peek doesn’t remove the top element dr. Amal Khalifa, Spr 17

Example Printing elements without modification property Count obtains the number of elements in stack. Although Fig. 21.6 does not demonstrate it, class Stack also has method Contains, which returns true if the Stack contains the specified object, and returns false otherwise. dr. Amal Khalifa, Spr 17

Output dr. Amal Khalifa, Spr 17

Class Hashtable dr. Amal Khalifa, Spr 17

What is a Hashtable? When we convert a key into an array index, we literally scramble the bits, making a “hash” of the number. dr. Amal Khalifa, Spr 17

Why Hashtable ? Storing Large data, efficient retrieve, saving memory space Example: 100 employees with nine-digit social security numbers you want to store and retrieve employee data by using the social security number as a key nominally require an array with 1,000,000,000 elements. Hashing: a high-speed scheme for converting keys such as social security numbers and inventory part numbers to unique array indices. dr. Amal Khalifa, Spr 17

Hashing when an app needs to store something, the scheme could convert the key rapidly to an index and the record of information could be stored at that location in the array Retrieval occurs the same way— but in reverse. dr. Amal Khalifa, Spr 17

Collisions two different keys “hash into” the same cell, or element, in the array we cannot sort two different data records to the same space Solutions: “hash again” - find an alternative home for all records beyond the first that hash to a particular array index. hash “bucket” - a linked list of all the key–value pairs that hash to that cell dr. Amal Khalifa, Spr 17

Hash Function A hash function performs a calculation that determines where to place data in the hash table. The hash function is applied to the key in a key– value pair of objects. Class Hashtable can accept any object as a key. class object defines method GetHashCode, which all objects inherit. dr. Amal Khalifa, Spr 17

Example Creating a HashTable of <string, int> dr. Amal Khalifa, Spr 17

Example unbox and box the int data stored in the Hashtable every time it incremented the count for a particular key  inefficient Downcasting boxing & untboxing If a word appears twice, line 38 will try to downcast this string to an int, causing an InvalidCastException at execution time. The error that appears at execution time will indicate that the problem is at line 38, where the exception occurred, not at line 42. This makes the error more difficult to find and debug, especially in large apps where the exception may occur in a different file—and even in a different assembly. dr. Amal Khalifa, Spr 17

Example If we need to use any of its string- specific methods, we need an explicit downcast. The enumerator of a Hashtable uses the DictionaryEntry structure to store key–value pairs. dr. Amal Khalifa, Spr 17

dr. Amal Khalifa, Spr 17

Take care!! dr. Amal Khalifa, Spr 17

That’s all,,,, Chapter 21: 21.1  21.4 dr. Amal Khalifa, Spr 17

Case Study dr. Amal Khalifa, Spr 17

dr. Amal Khalifa, Spr 17

dr. Amal Khalifa, Spr 17

dr. Amal Khalifa, Spr 17