Modern Collections Classes & Generics

Slides:



Advertisements
Similar presentations
Data Structures Static and Dynamic.
Advertisements

1 Chapter-01 Introduction to Computers and C++ Programming.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Intermediate 2 Computing Unit 2 - Software Development Topic 2 - Software Development Languages and Environments.
Compilers and Interpreters. HARDWARE Machine LanguageAssembly Language High Level Language C++ Visual Basic JAVA Humans.
Compilers and Interpreters
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Chapter 8: Understanding Collections Textbook: Chapter 4.
CIS 234: Object-Oriented Programming with Java
CMSC 202 ArrayList Aug 9, 2007.
Using the Java Collection Libraries COMP 103 # T2
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Lecture 10 Collections Richard Gesick.
Why don’t programmers have to program in machine code?
CSC 222: Object-Oriented Programming
Chapter 10 Programming Fundamentals with JavaScript
CS 440 Database Management Systems
High and low level languages
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Unit 5 Lesson 3: Introduction to Arrays
Arrays Low level collections.
F453 Computing Questions and Answers
Chapter 5 Ordered List.
ADT’s, Collections/Generics and Iterators
Modern Collections Classes
Database Systems Unit 16.
records Database Vocabulary It can be useful to collect information.
Testing and Debugging.
Generics and Subtyping
Chapter 7 Part 1 Edited by JJ Shepherd
Using Destiny Report Builder to Count Books Added to Collection Sorted by Funding Source 3/24/16.
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.
Cse 373 April 12th – TreEs.
Interfaces and Inheritance
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
structures and their relationships." - Linus Torvalds
MIS Professor Sandvig MIS 324 Professor Sandvig
TRANSLATORS AND IDEs Key Revision Points.
Chapter 10 Programming Fundamentals with JavaScript
Chapter 5 Ordered List.
Algorithm design and Analysis
Introduction to the Standard Template Library
Building Java Programs
LESSON 13 – INTRO TO ARRAYS
Object-Oriented Programming
CMSC 202 ArrayList Aug 9, 2007.
Java Programming Arrays
searching Concept: Linear search Binary search
Lesson 12.
Modern Collections Classes
CMSC 202 ArrayList Aug 9, 2007.
Building Java Programs
ITEC 2620M Introduction to Data Structures
MIS Professor Sandvig MIS 324 Professor Sandvig
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
Introduction to Data Structure
Building Java Programs
The Generic List<> Collection class
Data Structures & Algorithms
Intro to Collections.
Troubleshooting Compiler Errors
1.3.7 High- and low-level languages and their translators
Java String Class String is a class
Performance Measurement
structures and their relationships." - Linus Torvalds
Hello World Program In Visual Studio and Debugging
Introduction to Java Collection
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Modern Collections Classes & Generics

Collection Classes

What is a ‘Collection Class’ A class whose purpose is storing a collection of objects

What is a ‘Collection Class’ An array is a collection of objects. Technically it’s not a class  Arrays have lots of limitations: Hard to find items (unless it’s sorted  binary search) Hard to add new items to the middle of the array Hard to add new items once the array is full Arrays are best storing a fixed number of a specific type (class)

What is a ‘Collection Class’ A collection class makes it easy to store lots of objects Specific classes may have unique advantages (and disadvantages) E.g., “generic” collection classes can be used with any one type, in a type-safe way (this means that the compiler can check that you’re only using that specific type, at compile time)

Examples of Collections classes The ‘Classic’ collections These were included in C# 1.0. ‘Generics’ weren’t added until C# 2.0 These can store anything; they don’t check that you’re storing the right type of data Examples: System.Collections.ArrayList System.Collections.Stack System.Collections.Queue

Examples of Collections classes The ‘Generic’ collections These were added in C# 2.0. These check that you’re storing the right type of data (Actually, the compiler checks; compile time checking is better than run-time checking) Examples: System.Collections.Generics.List<> System.Collections.Generics.Stack<> System.Collections.Generics.Queue<> System.Collections.Generics.Dictionary<>

Examples of Collections classes Specialty collections It’s good to know that more collections exist, but don’t worry about knowing them Examples: Anything in Sytem.Collections.Concurrent.*

Generics

Why Were Generic Classes Added To C#? Some programming language features are for us, the programmers Flow control: if, while loops, for loops Classes Etc.

Why Were Generic Classes Added To C#? Some programming language features are for people who make the libraries that we use “Generics” were added to C# to improve ‘Collections’ classes A ‘collection’ class stores a bunch of data An array can be thought of as a collection of data There’s lots of other ways of organizing data (other collections) We can use generic classes to improve our code, but it’s not the sort of thing you’ll use all day every day

What is a generic class? (What is a generic data type?) A class that can be used with several different types of data A ‘normal’ class only works with a single, specific type of data If a ‘normal’ class wants to work with more than one type of data then it will have to use inheritance to accept a specific type of data OR ANY SUBCLASS of that type

What is a generic class? (A.k.a., a generic data type?) Example: ArrayList() vs. List<int>() We’ll look at code in Visual Studio Key point: ArrayList() accepts anything Makes it easier to accidentally put the wrong data in Makes it more complicated to get the data back out Key point: List<int>() will only accept ints. Compiler stops us from accidentally putting the wrong data in Easier to get the data back out We can also do List<DemoClass>(), or List<double>(), or….