Lecture 10 Collections Richard Gesick.

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Generics, Lists, Interfaces
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
 2006 Pearson Education, Inc. All rights reserved Collections.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
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.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lists in Python.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
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.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
CSE 1201 Object Oriented Programming ArrayList 1.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Understanding Data Types and Collections Lesson 2.
CMSC 202 ArrayList Aug 9, 2007.
Chapter 11 - JavaScript: Arrays
Chapter 5 Ordered List.
Arrays (review) CSE 2011 Winter May 2018.
Introduction to LINQ and Generic Collections
Review Deleting an Element from a Linked List Deletion involves:
Strings, StringBuilder, and Character
Chapter 7: Working with Arrays
Containers and Lists CIS 40 – Introduction to Programming in Python
Basic Data Structures.
String String Builder.
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.
Java Review: Reference Types
Searching.
Interfaces and Inheritance
Starting Out with C++ Early Objects Eighth Edition
structures and their relationships." - Linus Torvalds
CS313D: Advanced Programming Language
Programming in Java Lecture 11: ArrayList
Lecture 10 List Richard Gesick.
Lecture 22 Inheritance Richard Gesick.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Arrays and Collections
CMSC 202 ArrayList Aug 9, 2007.
CIS16 Application Development and Programming using Visual Basic.net
Can store many of the same kind of data together
Dynamic Data Structures and Generics
Object Oriented Programming in java
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
CMSC 202 ArrayList Aug 9, 2007.
Data Structures (CS212D) Week # 2: Arrays.
ArrayLists 22-Feb-19.
Collections Framework
Dynamic Data Structures and Generics
Introduction to Data Structure
CIS 199 Final Review.
Data Structures & Algorithms
Fundaments of Game Design
CSE 143 Lecture 2 ArrayIntList, binary search
Standard Template Library
Standard Template Library
Java String Class String is a class
Arrays and ArrayLists.
structures and their relationships." - Linus Torvalds
Presentation transcript:

Lecture 10 Collections Richard Gesick

Objectives The nongeneric and generic collections that are provided by the .NET Framework. To use class Array’s static methods to manipulate arrays. To use the foreach statement with the .NET collections. To use nongeneric collection classes ArrayList To use generic collection class List<T>

Introduction For the vast majority of applications, there is no need to build custom data structures. Instead, you can use the prepackaged data-structure classes provided by the .NET Framework. These classes are known as collection classes—they store collections of data. Each instance of one of these classes is a collection of items. Collection classes enable programmers to store sets of items by using existing data structures, without concern for how they are implemented. Programmers can code faster and expect excellent performance, maximizing execution speed and minimizing memory consumption.

Intro (2) The .NET Framework provides three namespaces dedicated to collections: System.Collections contains collections that store references to objects. Most new applications should use the collections in the System.Collections.Generic namespace, which contains generic collection classes. The System.Collections.Specialized namespace contains several collections that support specific types, such as strings and bits.

Intro (3) The collections in these namespaces provide standardized, reusable components; you do not need to write your own collection classes. These collections are written for broad reuse and tuned for rapid execution and for efficient use of memory.

Arrays Arrays are good for fast/immediate access to the data, but their limitation is that they are fixed in size.  To overcome this, collections that are dynamic allow us to grow/shrink the data structure. High-level APIs provide a means to access these dynamic collections similar to arrays (and use the [] index notation), but realize that there is a lot of memory management that is happening behind the scenes.

Arrays(2) All arrays implicitly inherit from abstract base class Array (namespace System). Class Array defines property Length, which specifies the number of elements in the array. In addition, class Array provides static methods that provide algorithms for processing arrays. Many of these methods have several overloaded versions.

Static Methods of Arrays When Array method Sort returns, the array contains its original elements in ascending order. int[ ] dblValues = { 3, 54,12, 3, 45, 78, 65}; Array.Sort(dblValues); Use Array method Copy to copy elements from one array to another. The first argument is the array to copy. The second argument is the destination array. The third argument is an int representing the number of elements to copy. Array.Copy(dblValues,ddblCopy,dblValues.Length);

Static Methods of Arrays(2) Array method BinarySearch performs a binary search on a sorted array. Method BinarySearch receives the sorted array in which to search and the key for which to search. int result = Array.BinarySearch(dblValues,6); The method returns the index in the array at which it finds the key, or a negative number if the key was not found. Its behavior on an unsorted array is unpredictable.

Other static Array methods include: Clear sets a range of elements to 0, false or null. CreateInstance creates a new array of a specified type. IndexOf locates the first occurrence of an object in an array or portion of an array. LastIndexOf locates the last occurrence of an object in Reverse reverses the contents of an array or portion of an array.

ArrayList Many languages provide a means to manage polymorphic collections such as the ArrayList This is polymorphic in that it holds elements of type "object", and all classes implicitly inherit from "object".  This is what provides the "Equals()", "ToString()", etc. methods that all things have.

ArrayList (2) ArrayList collection = new ArrayList(); collection.Add(42); collection.Add("Dog"); collection.Add(new Vector2(5,7)); This is permissible because the int, string, and Vector2 class are all instances of the "object" class (via inheritance).

ArrayList(3) The problem with the ArrayList (and any polymorphic collection) is that you must be careful in handling the data when you access it. You could not, for example do something like this (since all elements may not be ints): foreach(int i in collection) {   // do something } This may generate an invalid typecast exception.

ArrayList Meth-ods

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.

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.

List The Contains method returns true if the element is found in the List, and false otherwise. Contains compares its argument to each element of the List in order, so using Contains on a large List is inefficient. The Capacity property indicates how many items the List can hold without growing. List is implemented using an array behind the scenes. When the List grows, it must create a larger internal array and copy each element to the new array. A List grows only when an element is added and there is no space for the new element. The List doubles its Capacity each time it grows.

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;