5. Collections - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

5. Collections

Description:

Collections are ways of storing groups of objects. Different collection types have different properties ... A jagged array has sub-arrays of variable length. ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 28
Provided by: gordon48
Category:

less

Transcript and Presenter's Notes

Title: 5. Collections


1
5. Collections
  • Arrays
  • Other basic data structures
  • .NET collections
  • Class library example

2
Arrays
  • Collections are ways of storing groups of
    objects. Different collection types have
    different properties that make them suitable for
    different situations.
  • The most basic collection type is an array.
  • Support for arrays are built in to most
    languages.
  • Arrays are single entities with a single name
    that refer to multiple objects of the same type.
  • The elements of an array are referred to by an
    array index or subscript that identifies the
    current item in the array.

3
Arrays
  • Declaring an array - example
  • int a
  • In this case a variable called a is a
    reference or placeholder for an array of
    integers.
  • It can be pointed to an array object,
    initialized with the new operator
  • a new int5
  • Elements in the array can then be accessed using
    the subscript in brackets. Subscripts start at
    zero
  • a2 5

4
Arrays
  • Arrays normally go hand-in-hand with loops
  • for(int i0 ilt5 i)
  • ai i1
  • C has an additional keyword just for iterating
    over collections, foreach
  • foreach(int i in a)
  • MessageBox.Show(ai.ToString())

5
Arrays
  • In C, arrays also have a built-in length
    property (more on properties later)
  • for(int i0 ilta.Length i)
  • ai i
  • There are also built-in static methods in the
    System.Array class that are useful, such as
    Sort(), IndexOf(), Reverse(), etc.
  • if (Array.IndexOf(a, 4) 4)
  • MessageBox.Show(Bingo!)
  • Note that the method is part of the Array class,
    not a specific array.

6
Arrays
  • Array members are assigned a default value.
    Numbers are assigned zero, and reference types
    are assigned null. Arrays can be initialized in
    one step, for example
  • int myIntArray new int5 5,6,7,8,9
  • Or
  • int myIntArray 5,6,7,8,9
  • C has a keyword params that lets you pass a
    variable number of parameters to a method and
    treat them as an array, for example
  • DisplayValues(1,2,3,4,5)
  • public void DisplayVals(params int intVals)
  • foreach(int i in intValues)
  • MessageBox.Show(i.ToString())

7
Arrays
  • Each array element can also be an array, which
    results in a multidimensional array.
  • A rectangular array has sub-arrays of equal
    length. A jagged array has sub-arrays of
    variable length.
  • Rectangular arrays are especially useful for
    storing anything that has rows and columns, like
    the pixels in an image, or rows and columns in a
    spreadsheet.

8
Arrays
  • Iterating over a multi-dimensional array
    typically involves creating a loop for each
    dimension.
  • const int iRows 3
  • const int iCols 3
  • int, matrix new intiRows, iCols
  • for (int i0 iltiRows i)
  • for (int j0 jltiCols j)
  • matrixi, j i j
  • MessageBox.Show((matrix0,0 matrix2,2).ToStri
    ng())

9
Arrays
  • A bitmap image can be thought of as a
    two-dimensional array of pixel values. This
    example transforms an image into a grayscale
    image containing the blue component of each pixel
    value
  • // Bitmap is a built-in .NET object
  • Bitmap curBitmap new Bitmap(mapPictureBox.Image)
  • int yMax curBitmap.Size.Height
  • int xMax curBitmap.Size.Width
  • for (int y 0 y lt yMax y)
  • for (int x 0 x lt xMax x)
  • Color c curBitmap.GetPixel(x, y)
  • curBitmap.SetPixel(x, y, Color.FromArgb(c.B,
    c.B, c.B))
  • mapPictureBox.Image curBitmap

10
Arrays
  • The contents of an array can be object
    references.
  • The references must be of the same type.
  • // Line object is programmer-defined.
  • Line myLine new Line5
  • for (int i 0 i lt 5 i)
  • myLinei new Line()

11
Arrays
  • Arrays have the advantage of performing well, and
    being simple to use.
  • However, they are hard to restructure. For
    example, deleting an item requires moving all the
    subsequent items and shortening the array.
    Inserting requires moving all the subsequent
    items and lengthening the array.
  • They are best used to store of things that are
    relatively static in their structure.

12
Linked List
  • A linked list is a classic data structure that
    solves some of the problems of arrays.
  • Each node in a list has a pointer (or reference)
    to the next node in the list

13
Linked List
  • To insert an item, the adjacent items need only
    have their pointers updated. Deleting an item
    just entails changing the neighboring references

14
Tree
  • A tree is another classic data structure often
    used in algorithms related to sorting or indexing
    data. Each node contains a reference to one
    parent and one or more children
  • There are many varieties of trees, such as binary
    trees (each node has two children), quadtrees
    (each node has four children), and many others
    (more details here).

15
Graph
  • A graph is a more general case of a set of
    interconnected nodes (more details here). Graphs
    are used in network modeling and there are many
    related algorithms in areas such as route
    selection

16
.NET Collections
  • In day-to-day programming, you normally dont
    have to implement linked lists, trees, or graphs,
    but they are fundamental to many basic computer
    science algorithms.
  • The .NET framework includes multiple built-in
    data structures that will meet most routine
    programming requirements.
  • The ArrayList is a very useful built-in
    collection. It behaves like both an array and a
    list.
  • Items are ordered like an array, but can be
    inserted and deleted easily.

17
.NET Collections
  • ArrayList example
  • ArrayList myList new ArrayList()
  • for (int i 0 i lt 5 i)
  • myList.Add(i 1.5)
  • for (int i 0 i lt myList.Count i)
  • MessageBox.Show(i.ToString())

18
.NET Collections
  • Example of a class that uses an ArrayList to
    store geometry
  • Note the typecast required to treat the
    ArrayList item as a geometry object.
  • Array list elements are treated as type Object,
    so need to be cast to be used as any particular
    type.

19
.NET Collections
  • Note that the ForEach keyword removes the need
    for the typecast within the loop

20
.NET Collections
  • In version 1.x of .NET, collection classes were
    un-typed. That is, all of the stored objects
    were of type Object.
  • That means, each item must be cast to the
    applicable type before it is used.
  • C 2.0 adds support for Generics, which allow you
    to create typed collections.
  • The List class is the .NET 2.x equivalent of the
    ArrayList (ArrayList is still available).

21
.NET Collections
Note the lt gt syntax where the list is declared.
This defines the list to contain only elements of
the Geometry type. Using the generic syntax, the
List is now typed correctly, and there is no need
to cast it from the Object type to Geometry.
22
.NET Collections
  • Other built-in collections that are available
    include HashTable (.NET 1.x) and Dictionary (.NET
    2.x). These are ideal for storing values that
    are referenced by another value. Each item has a
    key and value, and the key is used to locate the
    value

23
.NET Collections
  • Other built-in collections that are available
    include Stacks and Queues. Stacks are
    last-in-first-out

24
.NET Collections
  • Queues are first-in-first-out. Message queues,
    where each message is processed in turn, is a
    typical application of a queue

25
Designing Classes
  • With collections and objects, we now have
    everything we need to design a simple geometry
    class library.
  • Starting with a basic point and line classes

26
Designing Classes
  • We can now add support for objects requiring
    multiple points or lines, such as polygons or
    arbitrary collections of geometry objects.

27
Designing Classes
  • These examples are not realistic for a real
    class library, but enough to illustrate some
    object oriented programming concepts.
  • A more complete geometry class library can be
    found in, for example, an implementation of the
    Simple Features Spec.
Write a Comment
User Comments (0)
About PowerShow.com