0% found this document useful (0 votes)
34 views

Collections: A Tutorial

Collections allow storing data of different types in a linked list of unknown length. Collections can contain zero or more elements and allow duplicates. Common collection operations include adding an item, getting the next item, resetting iteration, checking for more elements, and checking if empty. Unlike arrays, collections have a resizable size and do not require predetermining the number of elements. Sample code shows using a collection to loop through stored items. Questions ask about copying a collection to an array excluding duplicates, outlining steps to search a collection for the next print job based on time and priority level, and using a collection to store radio station names and frequencies.

Uploaded by

Samiksha More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Collections: A Tutorial

Collections allow storing data of different types in a linked list of unknown length. Collections can contain zero or more elements and allow duplicates. Common collection operations include adding an item, getting the next item, resetting iteration, checking for more elements, and checking if empty. Unlike arrays, collections have a resizable size and do not require predetermining the number of elements. Sample code shows using a collection to loop through stored items. Questions ask about copying a collection to an array excluding duplicates, outlining steps to search a collection for the next print job based on time and priority level, and using a collection to store radio station names and frequencies.

Uploaded by

Samiksha More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Collections

A tutorial

What are they? How do we use them? How are they different from arrays?
COLLECTIONS:
Linked lists of unknown
length or size.
Characteristics of collection

➔ Unknown length or size


➔ Can consist of zero or more elements
➔ Can contain data of different types
➔ Allow duplicate elements
➔ Can be ordered or unordered
Collections: Pseudocode Syntax

● addItem(): Used to add an item in the collection. The data


to be added goes in the bracket
● getNext(): Returns the next item in the collection
● resetNext(): Resets the iteration of the loop to the first
item in the collection
● hasNext(): Checking if there are more elements in the
collection (returns true or false)
● isEmpty(): Checking if the collection is empty (Returns
true or false)
Collection vs Array

● Collections allows the storage of data even if they are in


different data types. (non-generic collections)
● Furthermore, unlike an array, the number of elements in
the collection can be changed (They act like resizable
arrays); the programmer does not need to know in
advance the number of items that will be placed in the
data structure.
Sample Collection

// STUFF is a collection that already exists


STUFF.resetNext()
loop while STUFF.hasNext()
ITEM = STUFF.getNext()
// process ITEM in whatever way is needed
end loop
Questions
Question 1 (6 Marks)

Copy a collection into


an array.
(Exclude duplicates)
Question 2 (6 Marks)

At a small start-up firm, one printer serves as a printer for the


entire office, so printing jobs for more important members of
the firm have priority. After 15 minutes in the cue, a job is
automatically put next on the printing cue. A collection holds a
list of jobs for the printer. Objects in this collection hold the
time when the print job was sent as well as its priority level
from 1 (highest priority) to 3 (lowest priority).

Outline the steps needed to search this collection and return


the next job to be printed.
Question 3 (3 Marks)

In a car, the radio system can access 6 frequencies. Above


represents an array known as radio which stores the
frequencies of 6 different radio stations. A screen in the car
also needs to show the name of each radio station.

Show how a collection can be used to store both the name and
frequency data of each radio station.
Answers
Question 1:
Question 2:

Accept answer in bullet points, algorithm, or in text form with


logical ordering

Award 1 mark for each point included:

- Looping collection
- If present, returning job over 15 minutes first
- Searching loop again for every priority level
- Searching priority level 1-3 in order
- End when highest priority level job is found

You might also like