Lecture 10: Collections.

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

Fun with Lists for the Novice and Expert Scott Reed Brain Hz Software (760)
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
 2006 Pearson Education, Inc. All rights reserved Collections.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
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.
Iterators in Java. Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
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.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Object Oriented Software Development 6. Arrays and collections.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
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.
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.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Lecture 10 Collections Richard Gesick.
Advanced .NET Programming I 2nd Lecture
Chapter 6: Using Arrays.
Computing with C# and the .NET Framework
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Sequences and Iterators
University of Central Florida COP 3330 Object Oriented Programming
COMP 121 Week 9: ArrayList.
Inheritance & Polymorphism
ARRAYLIST AND VECTOR.
Iterators and Comparators
Chapter 5: Programming with C#
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
Creating and Using Classes
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
CS313D: Advanced Programming Language
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Repeating Instructions And Advance collection
List Data Structure.
Arrays and Collections
Object-Oriented Programming Paradigm
Collections Not in our text.
Statement-Level Control Structures
Managing Collections of Data
CSE 1020: The Collection Framework
CHAPTER 21 LOOPS 1.
Fundaments of Game Design
Basic Collections.
5. 3 Coding with Denotations
Defining Interfaces using C#
Insertion Sort Array index Value Insertion sort.
Advanced .NET Programming I 3rd Lecture
GoTo IEnumerable considered harmful
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Lecture Set 9 Arrays, Collections, and Repetition
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

Lecture 10: Collections

Collections

Main Program (Animal Array) Console.WriteLine("Create an Array type collection of Animal " + "objects and use it:"); Animal[] animalArray = new Animal[2]; Cow myCow1 = new Cow("Deirdre"); animalArray[0] = myCow1; animalArray[1] = new Chicken("Ken"); foreach (Animal myAnimal in animalArray) { Console.WriteLine("New {0} object added to Array collection, " + "Name = {1}", myAnimal.ToString(), myAnimal.Name); } Console.WriteLine("Array collection contains {0} objects.", animalArray.Length); animalArray[0].Feed(); ((Chicken)animalArray[1]).LayEgg(); Console.WriteLine();

Main Program (Animal ArrayList Collection) Console.WriteLine("Create an ArrayList type collection of Animal " + "objects and use it:"); ArrayList animalArrayList = new ArrayList(); Cow myCow2 = new Cow("Hayley"); animalArrayList.Add(myCow2); animalArrayList.Add(new Chicken("Roy")); foreach (Animal myAnimal in animalArrayList) { Console.WriteLine("New {0} object added to ArrayList collection," + " Name = {1}", myAnimal.ToString(), myAnimal.Name); } Console.WriteLine("ArrayList collection contains {0} objects.", animalArrayList.Count); ((Animal)animalArrayList[0]).Feed(); ((Chicken)animalArrayList[1]).LayEgg(); Console.WriteLine(); Console.WriteLine("Additional manipulation of ArrayList:"); animalArrayList.RemoveAt(0); animalArrayList.AddRange(animalArray); ((Chicken)animalArrayList[2]).LayEgg(); Console.WriteLine("The animal called {0} is at index {1}.", myCow1.Name, animalArrayList.IndexOf(myCow1)); myCow1.Name = "Janice"; Console.WriteLine("The animal is now called {0}.", ((Animal)animalArrayList[1]).Name);

Limitations of Arrays

ArrayList Collection

The foreach Construct

Ienumerable is Implicit in foreach

C# Iterators – alternative definition Iterators in the .NET Framework are called "enumerators" and represented by the IEnumerator interface. IEnumerator provides a MoveNext() method, which advances to the next element and indicates whether the end of the collection has been reached; a Current property, to obtain the value of the element currently being pointed at; and an optional Reset() method, to rewind the enumerator back to its initial position. The enumerator initially points to a special value before the first element, so a call to MoveNext() is required to begin iterating. Enumerators are typically obtained by calling the GetEnumerator() method of an object implementing the IEnumerable interface. Container classes typically implement this interface. However, the foreach statement in C# can operate on any object providing such a method, even if it doesn't implement IEnumerable. Both interfaces were expanded into generic versions in .NET. The following shows a simple use of iterators in C# : // explicit version IEnumerator<MyType> iter = list.GetEnumerator(); while (iter.MoveNext()) Console.WriteLine(iter.Current); // implicit version foreach (MyType value in list) Console.WriteLine(value); http://en.wikipedia.org/wiki/Iterator

The Length & Count Properties

Collections Accessibility

Other Useful Methods in Collections Class

AddRange( )

IndexOf( )

to be continued...