Lecture 13 Dynamic Collections Richard Gesick

Slides:



Advertisements
Similar presentations
Data Structures(I) Circular Linked Lists Circular linked list A list in which every node has a successor; the last element is succeeded by the first element.
Advertisements

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
DATA STRUCTURE & ALGORITHMS
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Data structures Abstract data types Java classes for Data structures and ADTs.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Stacks And Queues Chapter 18.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Week 15 – Monday.  What did we talk about last time?  Tries.
An Array-Based Implementation of the ADT List
Lecture 10 Collections Richard Gesick.
Lecture 6 of Computer Science II
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Trees Chapter 11 (continued)
5.13 Recursion Recursive functions Functions that call themselves
CSCI-255 LinkedList.
Trees Chapter 11 (continued)
John Hurley Cal State LA
JAVA COLLECTIONS LIBRARY
Chapter 12: Data Structures
Data Structures Interview / VIVA Questions and Answers
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Data Structures and Database Applications Abstract Data Types
– Introduction to Object Technology
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Abstract Data Types (ADTs)
CS313D: Advanced Programming Language
CS1S467 GUI Programming LECTURE 11 Collections
Arrays and Linked Lists
Programming in Java Lecture 11: ArrayList
Lecture 10 List Richard Gesick.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Lesson Objectives Aims
Lecture 11 Memory Richard Gesick.
Further Data Structures
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Linked Lists.
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Object Oriented Programming in java
Managing Collections of Data
Arrays Week 2.
Introduction to Data Structure
The Generic List<> Collection class
Fundaments of Game Design
Dynamic allocation (continued)
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
(1 - 2) Introduction to C Data Structures & Abstract Data Types
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic Array: Implementation of Stack
structures and their relationships." - Linus Torvalds
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
CSE 303 Concepts and Tools for Software Development
Lecture 4 – Data collection List ADT
Presentation transcript:

Lecture 13 Dynamic Collections Richard Gesick Figures from Lewis, “C# Software Solutions”, Addison Wesley

Objectives Review static vs. dynamic collections Discuss dynamic collection API Utilize a collection (example)

Arrays Allocate a contiguous section of memory Homogeneous Access via index <type> NAME[]; NAME = new <type>[SIZE];

“Dynamic” Arrays Arrays are good if you know how large the collection will be But if this isn’t known beforehand, how big is enough? One approach: Allocate for N When N is exceeded Allocate 2N and copy N into new space Free up original space for N Time consuming (memory allocation/copy)

Reallocating an Array Original When full A A

Reallocating an Array Original When full A A B

Reallocating an Array Original When full A A B

Reallocating an Array Original When full A A B

Reallocating an Array Original When full A A B null

Reallocating an Array Original When full A Garbage Collection A B null

Collection APIs Let’s not reinvent the wheel Other APIs provide access to collections using System.Collection.Generic; List – single-reference (one-way) collection LinkedList – double-reference (two-way) Queue – first in, first out (FIFO) Stack – first in, last out (FILO) Dictionary – key/value pairs Methods to sort, search, iterate across these collections

Focus on List<T> List provides for growth (dynamic) Has capacity & count Capacity = size allocated Count = number of slots used Reallocated automatically Grows when full to 2x Shrinks when count = ½ capacity (shrinks to 2/3 capacity so we don’t “thrash”) Provides pseudo-immediate access Gory details are hidden from us

List API List<T>() - Constructor (to hold stuff of type T) Add(T) - Adds object of type T to end of list Remove(T) - Deletes first occurrence of match RemoveAt(int) - Removes the object at position Sort(…) – arranges the objects Clear() – removes all objects Insert(T, int) - Adds object at specified position Searching: Contains/Exists/Find/FindAll/FindIndex/FindLast

1301 Asteroids in XNA

A Motivating Example Asteroids Game What considerations must be made? Rocks/asteroids flying around Ship Missiles What considerations must be made?

Asteroids Considerations Collection of rocks Collection of missiles Rock - position, velocity Ship - position, movement, shield Missile - position, velocity Collision Detection

Summary Array List More details of C# List<T> Fixed in size (too small/large?) Immediate access via index Don’t forget to allocate each element if needed List Dynamic in size Pseudo-immediate access More details of C# List<T> http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx