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

Slides:



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

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Arrays.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
8 – Objects as building blocks Visual Basic: An Object Oriented Approach.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
Chapter 9 Introduction to Arrays
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Java Unit 9: Arrays Declaring and Processing Arrays.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Lecture No.01 Data Structures Dr. Sohail Aslam
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Lists in Python.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
Data Strcutures.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
LISTS AND ARRAYS CHAPTER Topics  C# Collections  List –Flexible collection of variable length  Array –Standard arrays  Multidimensional Arrays.
Data Structures Lecture 1: Introduction. Course Contents Data Types   Overview, Introductory concepts   Data Types, meaning and implementation  
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Understanding Data Types and Collections Lesson 2.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
REEM ALMOTIRI Information Technology Department Majmaah University.
Data Structure and Algorithms
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Chapter 6: Using Arrays.
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.
Multiple Dimension Arrays
7 Arrays.
Object Oriented Programming in java
Collections Framework
Presentation transcript:

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).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:here

.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. Simple Features Spec