Understanding Data Types and Collections Lesson 2.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
COMP 110 Introduction to Programming Mr. Joshua Stough.
TCSS 342, Winter 2005 Lecture Notes
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
Stacks, Queues, and Deques
Building Java Programs
Topic 3 The Stack ADT.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Object Composition Interfaces Collections Covariance Object class Programming using C# LECTURE 10.
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.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Data structures Abstract data types Java classes for Data structures and ADTs.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Understanding Data Types and Collections Lesson 2.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Introduction to Object-Oriented Programming Lesson 2.
Understanding General Software Development Lesson 3.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Object Oriented Software Development 6. Arrays and collections.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Understanding General Software Development Lesson 3.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Lecture 10 Collections Richard Gesick.
Cpt S 122 – Data Structures Abstract Data Types
Advanced Data Collections
Understanding Algorithms and Data Structures
Data Structure By Amee Trivedi.
Chapter 4 The easy stuff.
C# Programming: From Problem Analysis to Program Design
September 29 – Stacks and queues
Data Structures and Algorithms
Objectives In this lesson, you will learn to: Define stacks
Chapter 3: Using Methods, Classes, and Objects
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Cinda Heeren / Geoffrey Tien
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.
LINKED LISTS CSCD Linked Lists.
HW-6 Deadline Extended to April 27th
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
MIS Professor Sandvig MIS 324 Professor Sandvig
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
CS313D: Advanced Programming Language
Stacks and Queues.
Stacks, Queues, and Deques
Java Collections Framework
Introduction to Data Structures
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Data Structures and Algorithms
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Recall: stacks and queues
MIS Professor Sandvig MIS 324 Professor Sandvig
Introduction to Data Structure
Data Structures & Algorithms
Fundaments of Game Design
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Stacks and Queues.
LINEAR DATA STRUCTURES
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

Understanding Data Types and Collections Lesson 2

Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understanding and Using Different Data Types in the.NET Framework Understand and Use Different Data Types in the.NET Framework (2.5) Understanding Arrays and Collections Understand and Use Different Data Types in the.NET Framework (2.5) Understanding GenericsUnderstand Generics (2.6)

Data Types in C# The data type defines the size of memory needed to store the data and the kind of operation that can be performed on the data. C# is a strongly-typed language. That means the data types for variables, constants, literal values, method return values, and parameters must be known at the compile time.

Intrinsic Data Types Intrinsic data types are the primitive data types for which the support is directly built into the programming language. Data Type.NET Framework TypeRange boolSystem.Booleantrue or false charSystem.CharA Unicode character decimalSystem.Decimal to doubleSystem.Double e308 to e308 IntSystem.Int32-2,147,483,648 to 2,147,483,647 longSystem.Int64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 objectSystem.ObjectAn object stringSystem.Stringzero or more Unicode characters

Values Types and Reference Types A value type directly stores data within its memory. Reference types store only a reference to a memory location. The actual data is stored at the memory location being referred to. When you copy a reference type variable to another variable of the same type, only the references are copied. As a result, after the copy, both variables will point to the same object.

Casting In C#, you can cast an object to any of its base types. All classes in the.NET Framework inherit either directly or indirectly from the Object class. Assigning a derived class object to a base class object doesn’t require any special syntax: Assigning a base class object to a derived class object must be explicitly cast: At execution time, if the value of o is not compatible with the Rectangle class, the runtime throws a System.InvalidCastException.

Boxing and Unboxing Boxing is the process of converting a value type to the reference type. Unboxing is the process of converting an object type to a value type. For unboxing to succeed, you must cast using the same type that was used for boxing. If you use a different data type, you may might get InvalidCastException.

Arrays and Collections The collection data types provide data structures to store and manipulate a collection of items. These collection classes are defined as part of the System.Collections or System.Collections.Generic namespace. The arrays are special type of collections declared by using a programming language construct. All arrays are internally derived from the System.Array class. Some common collection data types are arrays, queues, stacks, and linked lists.

Arrays An array is a collection of items of the same type. The items in an array are stored in contiguous memory locations. Capacity of an array is predefined and fixed. Any array item can be directly accessed by using an index. C# array indexes are zero-based

Array - Internal Representation

Array – Common Operations Arrays support the following operations: –Allocation –Access The following code assigns a value of 10 to the fourth item of the array, and twice that value is then assigned to the variable calc:

Queues A collection of items in which the first item added to the collection is the first one to be removed. First In First Out (FIFO) Queue is a heterogeneous data structure. Capacity of a queue is the number of items the queue can hold. As elements are added to the queue, the capacity can be automatically increased.

Queues – Internal Representation

Queues – Common Operations Enqueue: Adds an item to the tail end of the queue. Dequeue: Removes the current element at the head of the queue. Peek: Access the current item at the head position without actually removing it from the queue. Contains: Determines whether a particular item exists in the queue.

Stacks A collection of items in which last item added to the collection is the first one to be removed. Last In First Out (LIFO) Stack is a heterogeneous data structure. Capacity of a stack is the number of items the queue can hold. As elements are added to the stack, the capacity can be automatically increased.

Stacks– Internal Representation A stack can be visualized just like the queue, except that the tail is called the top of the stack and the head is called the bottom of the stack. New items are always added to the top of a stack; when this happens, the top of the stack starts pointing to the newly added element. Items are also removed from the top of the stack, and when that happens, the top of the stack is adjusted to point to the next item in the stack.

Stack– Common Operations Push: Adds item to the top of the stack. Pop: Removes the element at the top of the stack. Peek: Access the current item at the top of the stack without actually removing it from the stack. Contains: Determines whether a particular item exists in the stack.

Linked Lists A linked list is a collection of nodes arranged so that each node contains a link to the next node in the sequence. Each node in a linked list contains of two pieces of information: –the data corresponding to the node –the link to the next node

Linked Lists – Internal Representation A singly linked list

Linked Lists – Internal Representation A doubly linked list

Linked Lists – Common Operations Add: Adds an item to a linked list. Remove: Removes a given node from the linked list. Find: Finds a node with a given value in the linked list.

Linked Lists – Visualizing the add operation Adding an item to a linked list is a matter of changing links.

Generics The Generics feature helps you define classes that can be customized for different data types. Generics provide many benefits including reusability, type-safety and performance. The most common use of generics is to create collection classes.

Generics – Definition and Instantiation The following code illustrates a simple definition of a generic class: The following code shows how to instantiate a generic class with a concrete type int :

Generics – Constraints Constraints specify restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. This declaration ensures that when an instance of EmployeeRecord is created, the only valid type arguments are of the type Employee or one of classes derived from the Employee class.

Generic Collections A generic collection is a collection that stores only the items of the same data type. To ensure type safety, the compiler checks that you can only assign a value of the correct data type from a generic collection. The generic collection types generally perform better than their non-generic equivalent. With generics there is no need to box and unbox each item.

Generic Collections Usage Generic collections are expressed using a special notation. For example, List is a generic List collection. The T in the notation must be replaced with the data type that you want to store in the collection

Common Generic Collections Some commonly used generic collections: Generic Collection Description List List is a strongly-types collection of items. Most commonly used as a general- purpose collection. Dictionary A strongly-type dictionary where you can put a value and its associated key. TKey specifies the data type of the keys in the dictionary and TValue specifies the data type of the values in the dictionary. This data type is often used for lookups because retrieving a value by using its key is very fast. Queue A first-in, first-out (FIFO) data structure. Items added to the queue first will be processed first. Queue is very useful for sequentially processing items as they arrive. Stack A last-in, first-out (LIFO) data structure. That is, items added to the stack last will be the one processed first. Stack has many applications in programming and computer science. SortedList A collection of key/value pairs that are sorted by the key and can be accessed both by the key and the index. SortedList provides very fast retrieval of data.

Recap Data types in the.NET Framework –Intrinsic data types –Value types and reference types –Conversion and casting –Boxing and Unboxing Arrays and collections –Single dimensional and multi-dimensional arrays –Collection classes Generics –Generic constraints –Generic Collections