Modern Collections Classes

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Generics, Lists, Interfaces
M1G Introduction to Programming 2 4. Enhancing a class:Room.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
240-Current Research Easily Extensible Systems, Octave, Input Formats, SOA.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
SharePoint document libraries I: Introduction to sharing files Why document libraries? Sharing files with others is essential to getting things done nowadays.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
TechKnowlogy Conference August 2, 2011 Using GoogleDocs for Collaboration.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
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.
CIS 234: Object-Oriented Programming with Java
Using the Java Collection Libraries COMP 103 # T2
Lecture 10 Collections Richard Gesick.
What you always wanted to know about life as a professional software developer but were too afraid to ask.
Generics, Exceptions and Undo Command
Chapter 5 Ordered List.
Modern Collections Classes
JAVA COLLECTIONS LIBRARY
Logging In ASP.Net Core 1.1.
CSE 373: Data Structures and Algorithms
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
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.
Introduction CSE 1310 – Introduction to Computers and Programming
Array.
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
structures and their relationships." - Linus Torvalds
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
CS313D: Advanced Programming Language
Dynamic Web Pages JavaScript Jill Thomas Oct 14, 2003.
Chapter 5 Ordered List.
Logging In ASP.Net Core 2.0.
Stacks and Queues.
Lesson Objectives Aims
ArrayLists.
Databases Lesson 2.
LESSON 13 – INTRO TO ARRAYS
Further Data Structures
Teaching London Computing
Java Programming Arrays
CSE 143 Lecture 2 Collections and ArrayIntList
Coding Concepts (Data Structures)
EE 422C Sets.
Lesson 12.
Object Oriented Practices
A Robust Data Structure
CSE 303 Concepts and Tools for Software Development
CS2110: Software Development Methods
slides created by Marty Stepp
Collections Framework
The Generic List<> Collection class
Data Structures & Algorithms
Fundaments of Game Design
Finding and using code online
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Ethan Apter
Linked Lists and Loops based on slides by Ethan Apter
CMPE212 – Reminders Assignment 2 due next Friday.
slides created by Ethan Apter
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Hardware is… Software is…
Why not just use Arrays? Java ArrayLists.
Modern Collections Classes & Generics
Lecture 3 – Data collection List ADT
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Modern Collections Classes

What is a ‘Collection Class’? A class whose purpose is storing a collection of objects Has useful methods BEYOND what a normal array provides Let’s look at an example MSDN (MicroSoft Developer Network) docs for List<> class

Let’s look at code List<string> dinosaurs = new List<string>(); Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity); dinosaurs.Add("Tyrannosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus"); dinosaurs.Add("Compsognathus"); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } List<> is really an array, with extra stuff to make our lives easier Add will resize the array when we run out of space Notice that we don’t specify the size of the List<> OUTPUT: Capacity: 0

Let’s look at code, Part 2 List<string> dinosaurs = new List<string>(); Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity); dinosaurs.Add("Tyrannosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus"); dinosaurs.Add("Compsognathus"); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Foreach can be used to access every element in the list OUTPUT: Tyrannosaurus Amargasaurus Mamenchisaurus Deinonychus Compsognathus

Let’s look at code, Part 3 Console.WriteLine("\nContains(\"Deinonychus\"): {0}", dinosaurs.Contains("Deinonychus")); Console.WriteLine("\nInsert(2, \"Compsognathus\")"); dinosaurs.Insert(2, "Compsognathus"); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } You can search the List – Contains, IndexOf, LastIndexOf, BinarySearch You can add new things into the middle of the list List<> will move stuff over to make space List<> will resize if needed Zero-based index OUTPUT: Contains("Deinonychus"): True Insert(2, "Compsognathus") Tyrannosaurus Amargasaurus Compsognathus Mamenchisaurus Deinonychus

Let’s look at code, Part 4 Console.WriteLine("\ndinosaurs[3]: {0}", dinosaurs[3]); Console.WriteLine("\nRemove(\"Compsognathus\")"); dinosaurs.Remove("Compsognathus"); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } You can access elements using an array-style syntax You can remove things List<> will move stuff over to fill in that space Only removed the first match. The second string is still there OUTPUT: dinosaurs[3]: Mamenchisaurus Remove("Compsognathus") Tyrannosaurus Amargasaurus Mamenchisaurus Deinonychus Compsognathus

Other useful methods Clear() – remove everything from the List<> Sort() – arrange everything from ‘smallest’ to ‘largest’ BinarySort() – once the List() is sorted, you can binary search the list in O(lgN) time There are lots (and lots and lots) of other methods

Brief Intro to / Review of Generic Classes

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: ‘normal’ ArrayList of Objects vs. generic List<int> Key point: ArrayList (of Objects) 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….

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

Collection Classes

What is a ‘Collection Class’ A collection class makes it easy to store lots of objects Specific classes may have unique advantages (and disadvantages)

Categories of Collections classes , Part 1 The ‘Classic’ collections These were included in C# 1.0. The language feature ‘Generics’ wasn’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

Categories of Collections classes, Part 2 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 because you’re “pre-paying” the cost of checking) Examples: System.Collections.Generics.List<> System.Collections.Generics.Stack<> System.Collections.Generics.Queue<> System.Collections.Generics.Dictionary<>

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

Collections - Important Concepts

Most (all?) languages provide these If it's not built into the language then you can probably find an open- source library that will provide these Some examples: Java Collections Framework  (diagram of the classes) Java vs. C# C# collections: laundry list of pre-defined, generic classes - probably not useful to learn from, but a good reference for when you start asking questions like "So - can I see a list of all my options?" Python ( from the python docs )

Client vs. Implementer distinction, Part 1 Client – server software: Web Browser ( client ) Web Server Client asks the server to do something Server does something, sends a response back to client Client never really knows exactly what the server does, just what the results are Client / Server Image from Wikimedia Commons

Client vs. Implementer distinction, Part 2 In the List<> example (with the dinosaurs), our software was a client of the List<> class. We didn’t make the List<> class We don’t even know how it works We use it, and it saves us time

Client vs. Implementer distinction , Part 3 Implementer: someone who makes something In software development “to implement…” means “to write code that does…” “To implement a linked list” means “to write a Linked List (in source code)”

Client vs. Implementer distinction , Part 4 ON THE JOB: If you can find a pre-made class to use, use it! (Re-)implementing something will cost you time and effort IN SCHOOL: Learning how to implement stuff will prepare you for higher-level, higher-paying jobs and careers

Learning Outcomes

Your job as a student learning Collections, Part 1 Know how to choose a good class Know how to learn the parts of the API you need Memorize the commonly used parts (like ‘Add’ and ‘Remove’, etc, for List<>)

Your job as a student learning Collections, Part 2 Know how to choose a good class Be able to compare/contrast the pros/cons of each Collection, given a particular problem to solve Have a general understanding of how each of the Collection classes that we look at in class are implemented internally, and use that knowledge when explaining pros/cons This is at the level of "I can recognize and explain a diagram/picture", NOT "I can write that code myself" Know how to learn the parts of the API you need Memorize the commonly used parts (like ‘Add’ and ‘Remove’, etc, for List<>)

Your job as a student learning Collections, Part 3 Know how to choose a good class Know how to learn the parts of the API you need In general, be able to write code that makes use of each of the Collections classes we look at Look at real-world docs and develop the ability to find what you need without getting overwhelmed This includes exploring stuff you’re not required to use in coding exercises.  I don't expect you to know how to code stuff that you didn't use in exercises, but I do expect you to know that they exist, and what they're supposed to do. Understand that programming changes over time, just like anything else, and be able to deal with this E.g., C# has the non-generic Collections namespace in Version 1 (“V1”) of .Net.  They added Collections.Generic in V2.  As a result, there's 'duplicate' classes in each. We're going to focus on the generic versions, because XYZ Similarly with Java - Hashtable is the older one, HashMap is the newer one.  Use HashMap when you can, but know that they're functionally equivalent (HashMap has been updated to be faster, implement the right interface(s), etc) Memorize the commonly used parts (like ‘Add’ and ‘Remove’, etc, for List<>)

Your job as a student learning Collections Know how to choose a good class Know how to learn the parts of the API you need Memorize the commonly used parts (like ‘Add’ and ‘Remove’, etc, for List<>) For this class you need to have these in your head. Knowing (at least) the basics will allow you to design your software quicker and better. I may ask exam questions like “What does this example code do?” that uses these methods One way to make a bad impression in an job interview: If you say that you “know how to use Collections” but then can’t remember any of the methods that are commonly used.