1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.

Slides:



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

Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Data Structures and Collections
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
C# Programming: From Problem Analysis to Program Design1 7 Arrays and Collections C# Programming: From Problem Analysis to Program Design 2 nd Edition.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 9/27/05CS360 Windows Programming Strings, Regex, Web Response.
C# Programming: From Problem Analysis to Program Design1 7 Arrays and Collections.
ArrayList, Multidimensional Arrays
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java SE 8 for Programmers, Third 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.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
LISTS AND ARRAYS CHAPTER Topics  C# Collections  List –Flexible collection of variable length  Array –Standard arrays  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.
5. Collections Arrays Other basic data structures.NET collections Class library example.
Understanding Data Types and Collections Lesson 2.
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.
Michael Olivero Microsoft Student Ambassador for FIU
CS360 Windows Programming
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Array and ArrayList ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items in an.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
CSE 1201 Object Oriented Programming ArrayList 1.
Introduction to C# By: Abir Ghattas Michel Barakat.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
CMSC 202 ArrayList Aug 9, 2007.
Lecture 10 Collections Richard Gesick.
Computing with C# and the .NET Framework
C# Arrays.
Chapter 5: Programming with C#
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.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
CS313D: Advanced Programming Language
CMSC 202 ArrayList Aug 9, 2007.
Data Structures and Database Applications Arrays And Lists
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Managing Collections of Data
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings

2 9/22/05CS360 Windows Programming Last Time  We began looking at GUI Programming  Completed talking about exception handling

3 9/22/05CS360 Windows Programming Arrays  Arrays are contiguous bytes of memory that store data elements that are accessed using an index into the array

4 9/22/05CS360 Windows Programming Arrays  Single dimension int[] myInts = new int[20];... Console.WriteLine(myInts[i]);  Multidimension string[,] myStrings = new string[5,6]; double[,,] myDoubles = new double[3,8,5];... Console.WriteLine(myDoubles[i,j,k]);  Jagged Point[][] myPolygons = new Point[3][]; myPolygons[0] = new Point[10]; myPolygons[1] = new Point[20]; myPolygons[2] = new Point[30];... for (int x = 0; x < myPolygons[1].Length; x++) Console.WriteLine(myPolygons[1][x]);

5 9/22/05CS360 Windows Programming Jagged Arrays  Each element in the array is itself an array Student 1 Student 2 Student 3 Student 25 Score 1Score 2Score 3 Score 1Score 2Score 3Score 4Score 5 Score 1Score 2 Score 1Score 2Score 3Score 4Score 5Score 6

6 9/22/05CS360 Windows Programming foreach Point[][] myPolygons = new Point[3][]; myPolygons[0] = new Point[10]; myPolygons[1] = new Point[20]; myPolygons[2] = new Point[30]; for (int x = 0; x < myPolygons[1].Length; x++) Console.WriteLine(myPolygons[1][x]); foreach (Point p in myPolygons[1]) Console.WriteLine(p); Note: inside a foreach loop we have read only access to the array elements.

7 9/22/05CS360 Windows Programming foreach  foreach statements can be used to iterate over the elements in a collection  Arrays in C# support the foreach statement  The syntax for foreach is the foreach keyword followed by parenthesis and o Type of element in the collection o Identifier name for element in collection o The keyword in o The identifier of the collection

8 9/22/05CS360 Windows Programming Another Example int [] MyArray; MyArray = new int [5]; MyArray[0] = 0; MyArray[1] = 1; MyArray[2] = 2; MyArray[3] = 3; MyArray[4] = 4; foreach(int ArrayElement in MyArray) System.Console.WriteLine(ArrayElement);

9 9/22/05CS360 Windows Programming Declaring Arrays  int[] myInts; o Creates a variable that can point to an array  myInts = new int[100]; o Creates an array of 100 ints. o These ints are initialized to 0, and stored, unboxed, in a memory block on the managed heap.  Control myControls; o Creates a variable that can point to an array  myControls = new Control[100]; o Creates an array of Control references, initialized to null. Since Control is a reference type, creating the array creates references—the actual objects are not created.

10 9/22/05CS360 Windows Programming Array Initialization  Single dimension o int[] myInts = new int[3] {1, 2, 3}; o int[] myInts = new int[] {1, 2, 3}; o int[] myInts = {1, 2, 3};  Multidimensional o int[,] MyArray = {{0, 1, 2}, {3, 4, 5}};

11 9/22/05CS360 Windows Programming Arrays are Derived from System.Array  int[] myInts = new int[3] {1, 2, 3};  double[,,] myDoubles = new double[3,8,5];  Properties o myInts.Length is 3 o myInts.Rank is 1 o myDoubles.Length is 120 o myDoubles.Rank is 3 o myDoubles.GetLength(1) is 8  Static Methods o Array.Sort(myInts); o Array.Sort(keys, items); o Array.Reverse(myInts); o Array.Clear(myInts); o int i = Array.IndexOf(myInts, 17);

12 9/22/05CS360 Windows Programming Collections  A useful namespace to know in the FCL is System.Collections  It contains classes that serve as containers for groups of data  Examples of the classes include: o ArrayList o BitArray o Hashtable o Queue o Stack

13 9/22/05CS360 Windows Programming Collections  Collections are Weakly Typed o They can store any kind of data  Primitive types  Any reference types o Could create an array of your own objects  Need to do a lot of type casting to access and use the data in the collections  Future versions of.NET will support generics o Equivalent to C++ templates

14 9/22/05CS360 Windows Programming Hash Tables  Hash tables are a form of storing data for easy and efficient retrieval  Items are stored in a table along with with their keys o Key/value pairs  Key’s are hashed to determine location in table

15 9/22/05CS360 Windows Programming Hash Tables  Hash table items are stored in System.Collections.DictionaryEntry objects  Methods in Hashtable include: o Add o Remove o Clear o ContainsKey o ContainsValue

16 9/22/05CS360 Windows Programming Example using System.Threading; using System.Windows.Forms; using System.Collections; class DaysOfTheWeek { public static void Main() { Hashtable days = new Hashtable(); days.Add( "Sunday", " الأحد " ); days.Add( "Monday", " الاثنين " ); days.Add( "Tuesday", " الثلاثاء " ); days.Add( "Wednesday", " الأربعاء " ); days.Add( "Thursday", " الخميس " ); days.Add( "Friday", " الجمعة " ); days["Saturday"] = " السبت "; string word = ""; foreach( DictionaryEntry entry in days ) word += entry.Key + " : " + entry.Value + "\n\n"; MessageBox.Show( word ); }

17 9/22/05CS360 Windows Programming Resizable Arrays  The arrays that we have seen so far are not resizable  System.Collections.ArrayList encapsulates dynamic arrays  Methods supported by ArrayList include: o Add o Clear o Clone (creates a shallow copy) o Remove o Sort

18 9/22/05CS360 Windows Programming Examples  Which is more efficient? Why? ArrayList list = new ArrayList(); for( int i = 0; i < ; i++ ) list.Add( i ); ArrayList list = new ArrayList(100000); for( int i = 0; i < ; i++ ) list.Add( i );

19 9/22/05CS360 Windows Programming Examples  To retrieve an item from an ArrayList o int i = (int) list[0]; o Why do we use (int)  To assign a value to an element in the array o list[0] = 333;

20 9/22/05CS360 Windows Programming Example  We have two options for looping through all the elements in the array o Count property o foreach for( int i = 0; i < list.Count; i++ ) Console.WriteLine( list[i] ); foreach( int i in list ) Console.WriteLine( i );

21 9/22/05CS360 Windows Programming More ArrayList Methods  Removing items from an array automatically shifts the items in the array up  Memory is not released as soon as an item is removed  To downsize an ArrayList to automatically fit the contents use TrimToSize method

22 9/22/05CS360 Windows Programming Example  Let’s look at the example in Figure 3.3 on pages of the book

23 9/22/05CS360 Windows Programming Strings  Another way of finding the classes available through the.NET FCL is the Object Browser o View menu, then Object Browser  You will find the classes under mscorlib  String is under mscorlib->System o It’s in the System namespace

24 9/22/05CS360 Windows Programming Strings  The methods by the String class include: o ToLower o ToUpper o Replace o Length o IndexOf

25 9/22/05CS360 Windows Programming Example string theString = "Programming Microsoft.NET"; Console.WriteLine("default: \t {0}", theString); Console.WriteLine("lowercase: \t {0}", theString.ToLower()); Console.WriteLine("uppercase: \t {0}", theString.ToUpper()); Console.WriteLine("replace: \t {0}", theString.Replace(".", "dot ")); Console.WriteLine("length: \t {0}", theString.Length); Console.WriteLine("\"Microsoft\" occurs at character {0}", theString.IndexOf("Microsoft")); Console.WriteLine(); Console.WriteLine("Please press enter key to quit"); Console.ReadLine();

26 9/22/05CS360 Windows Programming Summary  Arrays  Collections o Hash Tables o Resizable Arrays o Strings  Completed pages