Fundaments of Game Design

Slides:



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

Generics, Lists, Interfaces
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.
 2006 Pearson Education, Inc. All rights reserved Collections.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
TCSS 342, Winter 2005 Lecture Notes
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
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.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Understanding Data Types and Collections Lesson 2.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
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.
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.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
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.
Understanding Data Types and Collections Lesson 2.
Lecture 10 Collections Richard Gesick.
Sort & Search Algorithms
Advanced Data Collections
Understanding Algorithms and Data Structures
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Week 4 - Friday CS221.
Introduction to LINQ and Generic Collections
Containers and Lists CIS 40 – Introduction to Programming in Python
Standard Template Library (STL)
Basic Data Structures.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
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 17 Object-Oriented Data Structures
Road Map CS Concepts Data Structures Java Language Java Collections
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Basic Data Structures.
CS313D: Advanced Programming Language
structures and their relationships." - Linus Torvalds
Lecture 21 Stacks and Queues Richard Gesick.
Stacks and Queues.
Lecture 13 Dynamic Collections Richard Gesick
Lecture 10 List Richard Gesick.
Java Collections Framework
Object Oriented Programming in java
Collections Framework
Introduction to Data Structure
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Algorithms
Standard Template Library
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Standard Template Library
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Introduction to Computer Science
Presentation transcript:

Fundaments of Game Design C# Collections Richard Gesick

Objectives The nongeneric and generic collections that are provided by the .NET Framework. To use class Array’s static methods to manipulate arrays. To use the Dictionary, Stack, Queue classes To use key value pairs To use methods provided for the collection classes such as sort and contains

Introduction For the vast majority of applications, there is no need to build custom data structures. Instead, you can use the prepackaged data-structure classes provided by the .NET Framework. These classes are known as collection classes—they store collections of data. Each instance of one of these classes is a collection of items. Collection classes enable programmers to store sets of items by using existing data structures, without concern for how they are implemented. Programmers can code faster and expect excellent performance, maximizing execution speed and minimizing memory consumption.

Intro (2) The .NET Framework provides three namespaces dedicated to collections: System.Collections contains collections that store references to objects. Most new applications should use the collections in the System.Collections.Generic namespace, which contains generic collection classes. The System.Collections.Specialized namespace contains several collections that support specific types, such as strings and bits.

Intro (3) The collections in these namespaces provide standardized, reusable components; you do not need to write your own collection classes. These collections are written for broad reuse and tuned for rapid execution and for efficient use of memory.

Arrays(2) All arrays implicitly inherit from abstract base class Array (namespace System). Class Array defines property Length, which specifies the number of elements in the array. In addition, class Array provides static methods that provide algorithms for processing arrays. Many of these methods have several overloaded versions.

Static Methods of Arrays When Array method Sort returns, the array contains its original elements in ascending order. int[ ] dblValues = { 3, 54,12, 3, 45, 78, 65}; Array.Sort(dblValues); Use Array method Copy to copy elements from one array to another. The first argument is the array to copy. The second argument is the destination array. The third argument is an int representing the number of elements to copy. Array.Copy(dblValues,ddblCopy,dblValues.Length);

Static Methods of Arrays(2) Array method BinarySearch performs a binary search on a sorted array. Method BinarySearch receives the sorted array in which to search and the key for which to search. int result = Array.BinarySearch(dblValues,6); The method returns the index in the array at which it finds the key, or a negative number if the key was not found. Its behavior on an unsorted array is unpredictable.

Other static Array methods include: Clear sets a range of elements to 0, false or null. CreateInstance creates a new array of a specified type. IndexOf locates the first occurrence of an object in an array or portion of an array. LastIndexOf locates the last occurrence of an object in Reverse reverses the contents of an array or portion of an array.

Generic or Parameterized Collections To overcome the limitations of polymorphic collections, modern languages also provide a means by which you can explicitly state the type of thing you want the collection to manage. You do this via a "parameter" to the class when you define it. List<T> is called a generic class because it can be used with any type of object. T is a placeholder for the type of the objects stored in the list.

List The Add method appends its argument to the end of the List. The Insert method inserts a new element at the specified position. The first argument is an index—as with arrays, collection indices start at zero. The second argument is the value that is to be inserted at the specified index.

List The Count property returns the number of elements currently in the List. Lists can be indexed like arrays by placing the index in square brackets after the List variable’s name. The Remove method is used to remove the first instance of an element with a specific value. If no such element is in the List, Remove does nothing. RemoveAt removes the element at the specified index; all elements above that index are shifted down by one.

List The Contains method returns true if the element is found in the List, and false otherwise. Contains compares its argument to each element of the List in order, so using Contains on a large List is inefficient. The Capacity property indicates how many items the List can hold without growing. List is implemented using an array behind the scenes. When the List grows, it must create a larger internal array and copy each element to the new array. A List grows only when an element is added and there is no space for the new element. The List doubles its Capacity each time it grows.

Sorting with generics There is a sort method that is built into the array class and the generic classes. It uses the quick sort algorithm. If you want to sort information based on classes that you wrote, your class must provide an implementation for the IComparable interface.

Stacks Implement a first-in-last-out behavior Push() to add an element Pop() to remove an element Add and remove from the same side of the internal data structure Easiest to add/remove from the front if implemented as a linked list internally Peek() returns the top element without popping it from the stack

Stacks Which data structure would you pick to implement a Stack? Why?

Queues Implement a first-in-first-out behavior Enqueue() to add an element Dequeue() to remove an element Add and remove from the opposite sides of the internal data structure If we maintain a list-tail reference, then we can add to the end and remove from the front, each having constant - O(1) - time

Queues Which data structure would you pick to implement a Queue? Why?

Dictionary class The Dictionary class represents a data structure in C# that is used to represent a collection of key and value pairs of data. The Dictionary class is a generic class and can store any data types. It is in the System.Collections.Generic namespace. The Dictionary class constructor takes two parameters (generic type), first for the type of the key and second for the type of the value. This is also known as a key-value pair. Dictionary<string, int> sub = new Dictionary<string, int>();

Dictionary methods Dictionaries don’t allow duplicate keys. If the key already exists and a key of the same value is added, an exception is thrown. Add: sub.Add(“A”, 5); the key is A and the value is 5 Item property works like an indexer so you can access values by calling the key Console.WriteLine("For key = \"D\", value = {0}.", sub["D"]);

More Dictionary methods The indexer can be used to change the value associated with a key. sub["D"] = 7; If a key does not exist, setting the indexer for that key adds a new key/value pair. sub["F"] = 19; To print a dictionary foreach (KeyValuePair<string, int> kvp in sub) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }