Collections 1. Two Hierarchies Recall that the architecture of an object- oriented program consists of two hierarchies: – The Class Structure (“is a”)

Slides:



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

Generics, Lists, Interfaces
Comparable and Comparator Interfaces Ben Rodes. Disclaimer.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Goal: To understand how to solve circuits with multiple power sources
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Polymorphism, Virtual Methods and Abstract Classes.
Inheritance, Shared. Projectiles Program Demonstrates – Inheritance – MustInherit – Shared vs. Non-shared methods A variation on the Multiball example.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Generics and Type Safety Recitation – 4/24/2009 CS 180 Department of Computer Science, Purdue University.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Graphics Procedure (IDrawable) 1. Basic Procedure For Drawing ThingsToDraw Create Objects From Classes Add Them to PictureBox Use the Paint Event to draw.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Collections, Part 2 You were introduced to Generic collections for assignment 3—ThingsToDraw was a Generic.List of Idrawable objects. Since the Generic.List.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Study Tips Some tips to help you succeed in Biology (or any other) class.
Classes and Class Libraries Examples and Hints November 9,
by Chris Brown under Prof. Susan Rodger Duke University June 2012
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Nachos Phase 1 Code -Hints and Comments
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
CS107 References and Arrays By Chris Pable Spring 2009.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
More on Polymorphism. Ever have one of those days?
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
Object Oriented Software Development
CSCI-383 Object-Oriented Programming & Design Lecture 18.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
CS2210: SW Development Methods Topics: Comparable and Comparator interfaces in JCF Function objects Textbook readings: More from MSD, Chapter 9 Pages
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Object-Oriented Principles Applications to Programming.
By Edward Lim 8.7. What? Today, we continued our research on our chosen Cornerstone Piece, we got our learning journals up to date, we made sure all our.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
Unit 6 Repetition Processing Instructor: Brent Presley.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
THE “COLLEGES I AM THINKING ABOUT” LIST IN YOUR FAMILY CONNECTIONS ACCOUNT.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
The ArrayList Data Structure Standard Arrays at High Speed!
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
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.
structures and their relationships." - Linus Torvalds
Macros/VBA Project Modules and Creating Add-Ins on the Toolbar
Arrays
structures and their relationships." - Linus Torvalds
Presentation transcript:

Collections 1

Two Hierarchies Recall that the architecture of an object- oriented program consists of two hierarchies: – The Class Structure (“is a”) – The Object Structure (“has a” or “has some”) One of the most important ways to implement the object structure in VB is using the built-in collection classes. Objects of one class can contain one or more collections of objects from other classes. 2

Arrays Arrays are useful and familiar—they are the type of collection that has been around the longest. Arrays have limitations: – Resizing an array requires the use of ReDim (and ReDim Preserve if there’s already data in the array that you want to save) – Inserting items into the middle of an array involves not only using ReDim Preserve, but moving all later elements back one in the array. – All items in an array must be of the same data type. 3

Array Examples Here are a few array examples. For more information on arrays, go to the Stephens book, pp. 620 to

.NET Collection Classes One of the nicest things that Microsoft programmers have ever done for us was to create a bunch of collection classes as part of the.NET framework. BTW, the.NET framework is the huge collection of classes that are available to you when you write your VB programs (the same classes are also available to C# programmers) 5

Collection Classes Chapter 28 of the textbook provides a detailed look at the many specialized collection classes available in VB, including ArrayList, LinkedList, HashTable, and more. Each one encapsulates a certain type of behavior that programmers used to spend hours or days coding into their programs. I will show you the collection classes that I have found most useful, but you should be aware that these other types exist—one of them may be just what you need for your project! 6

Non-Generic Collections The generic collections are much easier to use, since they require all objects to be of a single type. I prefer generic collections in my own code. However, you will find non-generic collections used throughout the.NET framework, so you will need to know how to use them. The most important non-generic collections you need to know about are: 1.The Items collection of a ListBox or ComboBox; 2.The Controls collection of a Form or container control (GroupBox, Panel, TabControl tab, for example) 7

The Items Collection ListBox and ComboBox controls have a property called “Items”. Items is a non-generic collection; it can hold all types of objects. Note: The meaning of “generic” here is pretty much the opposite of its meaning in English. Generic VB collections hold specific data types, while non-Generic collections hold any data type. Technically speaking, Items holds the addresses of the objects. So what do you see in the ListBox/ComboBox when you add a complicated object to Items? You see the value of the object’s ToString function. 8

Items Collection Okay, now we know how to add objects to ListBoxes and ComboBoxes, and have them say something meaningful. Why’d we do that? Both ListBoxes and ComboBoxes have a SelectedItem property. ListBoxes also have a SelectedItems collection, which is just the subset of Items that the user has selected. 9

SelectedItem Because you can put any type of object into a ListBox or ComboBox, we don’t necessarily know a lot about what type of object has been selected. Look what happens when I try to write code using SelectedItem: I can’t access SelectItem’s FirstName or GPA properties, because I’ve allowed any type of object into the ListBox, and I’m not sure that the user has picked one of type Student. To be able to access Student properties of SelectedItem, I have to: 1.Make sure that SelectedItem is a Student; and 2.Treat (cast) it as a Student. 10

TypeOf and DirectCast To verify that SelectedItem is a Student (or whatever class you want to check for), use TypeOf…Is. Once you know that it’s a student, use DirectCast to treat it as one. Example: 11

Too much code? While the flexibility of being able to put all types of objects into the Items collection (or any non- generic Collection class) is appealing, the fact that you have to use TypeOf and DirectCast all the time makes writing the code more difficult and time-consuming. In addition, since SelectedItem can be any type of object, Intellisense can’t help you out. It would be nice if we could make collections of objects of the same type—less flexible, but a lot easier! 12

Enter Generics This is where the Generic collections come in. Generic collections allow you to specify what data types are allowed into the collection. When you create a generic collection of a specific type, you can put in: 1.Objects of that class; or 2.Objects of any class inherited from the specified class. That is, if you make an Automobile class, you can add Fords, Toyotas and Volkswagens to it (assuming those classes inherit from Automobile). You can also create collections of an Interface type. These can hold objects of any class that implements the Interface. You will use at least one collection of an Interface type (ThingsToDraw) in Assignment 3. 13

Generic Collections As I said earlier, chapter 28 describes many different types of collections. We’ll focus on these: – Generic.List (today) – Generic.Queue (later) – Generic.Stack (later) – Generic.Dictionary (later) 14

Generic.List A Generic.List is basically an array on steroids (without the negative side effects). It is strongly typed, so you generally won’t need to use TypeOf and DirectCast with items in the List. It will only allow you to add objects of the correct type. You can add elements at the end of the list (Add), or anywhere else (Insert). You can check to see if an item is already in the List (contains), or search for items matching certain criteria (Find). You can easily convert the list to an array if you need to pass it as a parameter to some procedure (add “.ToArray” to the end of the List’s name). 15

Looping Through Lists Because the elements in a Generic.List are all of the same type, you can loop through them without checking their types. This makes the code much simpler: 16