Transcript Lecture 5

5. Collections
• Arrays
• Other basic data structures
• .NET collections
• Class library example
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.
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 int[5];
Elements in the array can then be accessed using the
subscript in brackets. Subscripts start at zero:
a[2] = 5;
Arrays
Arrays normally go hand-in-hand with loops:
for(int i=0; i<5; i++)
{
a[i] = i+1;
}
C# has an additional keyword just for iterating over collections,
foreach:
foreach(int i in a)
{
MessageBox.Show(a[i].ToString());
}
Arrays
In C#, arrays also have a built-in length property (more on properties
later):
for(int i=0; i<a.Length; i++)
{
a[i] = 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.
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 int[5] {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 DisplayValues(params int[] intVals)
{
foreach(int i in intVals)
{
MessageBox.Show(i.ToString());
}
}
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.
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 int[iRows, iCols];
for (int i=0; i<iRows; i++)
{
for (int j=0; j<iCols; j++)
{
matrix[i, j] = i + j;
}
}
MessageBox.Show((matrix[0,0] * matrix[2,2]).ToString());
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 < yMax; y++)
{
for (int x = 0; x < xMax; x++)
{
Color c = curBitmap.GetPixel(x, y);
curBitmap.SetPixel(x, y, Color.FromArgb(c.B, c.B, c.B));
}
}
mapPictureBox.Image = curBitmap;
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 Line[5];
for (int i = 0; i < 5; i++)
{
myLine[i] = new Line();
}
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.
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:
Linked List

To insert an item, the adjacent items need only have their
pointers updated. Deleting an item just entails changing the
neighboring references:
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).
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:
.NET Collections

In day-to-day programming, you normally don’t 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.
.NET Collections
ArrayList example:
ArrayList myList = new ArrayList();
for (int i = 0; i < 5; i++)
{
myList.Add(i * 1.5);
}
for (int i = 0; i < myList.Count; i++)
{
MessageBox.Show(i.ToString());
}
.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.
.NET Collections
Note that the ForEach keyword
removes the need for the typecast
within the loop:
.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).
.NET Collections
Note the < > 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.
.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:
.NET Collections
Other built-in collections that are available include Stacks and Queues. Stacks
are last-in-first-out:
.NET Collections
Queues are first-in-first-out. Message queues, where each message is processed
in turn, is a typical application of a queue:
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…
Designing Classes
We can now add support for objects requiring multiple points or lines, such as polygons
or arbitrary collections of geometry objects.
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.