Lecture 10 List Richard Gesick.

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Stacks - 2 Nour El-Kadri ITI Implementing 2 stacks in one array and we don’t mean two stacks growing in the same direction: but two stacks growing.
ArrayList, Multidimensional Arrays
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Understanding Data Types and Collections Lesson 2.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Understanding Data Types and Collections Lesson 2.
An Array-Based Implementation of the ADT List
Processing Sequences of Elements
Lecture 10 Collections Richard Gesick.
Review Array Array Elements Accessing array elements
Arrays, Lists, Stacks, Queues
Arrays (review) CSE 2011 Winter May 2018.
Introduction to LINQ and Generic Collections
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Lecture 7 Arrays, Dynamic Arrays & Copy Constructors “Absolute C++”
JAVA COLLECTIONS LIBRARY
Basic Data Structures.
Data Structures Interview / VIVA Questions and Answers
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
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.
This pointer, Dynamic memory allocation, Constructors and Destructor
Collections Intro What is the STL? Templates, collections, & iterators
Dynamically Allocated Memory
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
CS313D: Advanced Programming Language
Circular Buffers, Linked Lists
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Programming in Java Lecture 11: ArrayList
Lecture 13 Dynamic Collections Richard Gesick
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Lecture 11 Memory Richard Gesick.
LESSON 13 – INTRO TO ARRAYS
Further Data Structures
Arrays versus ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Linked Lists.
Object Oriented Programming in java
JavaScript Arrays.
CS2011 Introduction to Programming I Arrays (I)
Managing Collections of Data
ArrayLists 22-Feb-19.
Introduction to Data Structure
JavaScript: Arrays.
The Generic List<> Collection class
Data Structures & Algorithms
Fundaments of Game Design
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Dynamic Array: Implementation of Stack
Classes and Objects Object Creation
structures and their relationships." - Linus Torvalds
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
Python List.
Presentation transcript:

Lecture 10 List Richard Gesick

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

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.

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 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

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. All elements at the specified index and above are shifted up by one position.

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.

Using List List<int> numbers = new List<int>(); numbers.Add(42); numbers.Add(9); Since the type that the collection holds is known, we can do something like this: foreach(int i in numbers) {   Console.WriteLine(i); } And you can also access the elements via the [] index notation as in: numbers[0] = 8;

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