Download presentation
Presentation is loading. Please wait.
Published byΛυσιστράτος Ζερβός Modified over 6 years ago
1
CS1S467 GUI Programming LECTURE 11 Collections
2
Lecture Slides are unsuitable for learning how to program!
You just get an illusion of learning! Programming is a craft. You have to do it to learn it. 2
3
Review of Lab Session Sorting arrays Searching through arrays
4
Coursework 1 Deadline: Friday 15, 23:59, Blackboard Extra lab sessions
Read submission instructions Read the marking grid (load/save highscore? Chart used? Finding max and min values, ...) Don’t forget to include testing document installation documentation game rules document Extra lab sessions Wednesday 6 and 13, 13:00 to 15:00 J210 No lecture next Monday
5
Today Overview Queues & Stacks what data structure to use when
What they are methods how to use examples
6
Separating Boys from Men
Effective & appropriate use of data structures separates expert programmer from novice The expert programmer recognises that: organising data into appropriate data structures makes it easier to work with the data, using data abstraction makes it easier to come up with a good solution to programming problems.
7
Collections vs. Primitive Data Types
Primitive Data Types: int, float, char, …. not objects, can’t "do" things (sorting, searching) simple to use, access and understand "Hybrid": arrays and structs "constructors", get & set methods almost objects Collections: List, ArrayList, LinkedList, Queues, Stacks, … objects, can "do" things (sorting, searching) more difficult to use, access and understand
8
Two Collection Groups Linear Indexed
Lists, LinkedLists, Queues, Stacks, etc. fast sequential traverse slow random access Indexed ArrayLists, SortedLists, etc. slow sequential traverse fast random access
9
Collections: Overview (When to use What)
Queue Stack LinkedList SortedList List ArrayList Expand/shrink Add/remove fast slow Single use y (y) Sorting x Depends on algorithm Searching Random access Sequential access Example print queue 'Undo' large data sorting database Guide on these and more Collections:
10
Queues & Stacks Stacks Queues
Last-in-First-out (LiFo): data added and removed only from top of list used for expression evaluation function call handling 'Undo' functionality Queues First-in-First-out (FiFo): data are added at one end and removed from the other end of a list. prioritise operating system processes simulate events in the real world batch processing
11
Basic Stack Methods Push & Pop Peek
just looking at topmost without removing
12
Other Stack Methods Clear Clone Contains CopyTo, ToArray
completely empties the Stack Clone creates a copy Contains checks if an element is in the Stack (sequentially -> slow) CopyTo, ToArray copies into a 1D array Count (not a method, a property) amount of elements on the Stack Full list on:
13
How to Use the Stack Class
using System; using System.Collections; public class SamplesStack { public static void Main() // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push("World"); myStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine( "myStack" ); Console.WriteLine( "\tCount: {0}", myStack.Count ); Console.Write( "\tValues:" ); PrintValues( myStack ); } public static void PrintValues( IEnumerable mS ) { foreach ( Object obj in mS ) Console.Write( " {0}", obj ); }
14
Basic Queue Methods Enqueue & Dequeue Peek
just looking at first in queue without removing
15
Other Queue Methods (Identical to Stack)
Clear completely empties the Queue Clone creates a copy Contains checks if an element is in the Queue (sequentially -> slow) CopyTo, ToArray copies into a 1D array Count (not a method, a property) returns amount of elements in the Queue Full list on:
16
How to Use the Queue Class
using System; using System.Collections; public class SamplesQueue { public static void Main() // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue("Hello"); myQ.Enqueue("World"); myQ.Enqueue("!"); // Displays the properties and values of the Queue. Console.WriteLine( "myQ" ); Console.WriteLine( "\tCount: {0}", myQ.Count ); Console.Write( "\tValues:" ); PrintValues( myQ ); } public static void PrintValues( IEnumerable mQ ) { foreach ( Object obj in mQ ) Console.Write( " {0}", obj ); }
17
Example using System; using System.Collections;
public class Palindrome { public static void Main() bool isPalindrome=true; string ch; Stack myS = new Stack(); Console.WriteLine("Enter a word to check"); string wordToCheck = Console.ReadLine(); for (int x = 0; x < wordToCheck.Length; x++) myS.Push(wordToCheck.Substring(x, 1)); } int pos = 0; while (myS.Count > 0) ch = myS.Pop().ToString(); if (ch != wordToCheck.Substring(pos, 1)) isPalindrome = false; break; pos++; Example // Some variables we need // Create and initialise a new Stack // Ask user to enter a word to check // Push the word onto the stack, letter by letter // In effect the stack now holds the word BACKWARDS // Now compare (letter by letter) the word entered // to the reversed version on the Stack. If at any // point the letters are different, then the word is // not a palindrome.
18
Lists Lists are similar to arrays Either this way Or this way
Lists have to be given a size before use: Either this way Or this way (automatic run-time size-adjustment) (explicit size definition)
19
Lists Lists are also dissimilar to arrays
List are auto-initialised to zero List are not fixed in size. They can grow at run-time
20
Lists Lists are objects
They have methods (already used the .Add() method) Example: Searching
21
Lists Lists are objects
They have methods (already used the .Add() method) Example: Sorting Ah – and how to sort descending?
22
Lists Lists are objects
They have methods (already used the .Add() method) Example: Inserting
23
Lists and structs Lists can store anything
int, double, float, char, ..... arrays structs
24
Lists and structs Sorting a List of structs sort by what?
needs a sort definition
25
Lists and structs More after Christmas break
a 5 week project using Lists, structs and more visual components a record (or whatever) collection / database load, save, sort, search, ...
26
Reminder End of Lecture 11 Deadline: Friday 15, 23:59, Blackboard
Read submission instructions Read the marking grid (load/save highscore? Chart used? Finding max and min values, ...) Don’t forget to include testing document installation documentation game rules document Extra lab sessions Wednesday 6 and 13, 13:00 to 15:00 J210 No lecture next Monday End of Lecture 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.