Professional Documents
Culture Documents
Topic 4 Computetional Thinking-Collection
Topic 4 Computetional Thinking-Collection
Topic 4 Computetional Thinking-Collection
13
4.3.12 Discuss the need for sub-programmes and collections within programmed solutions.
Typically, they represent data items that form a natural group, such as
Why collections ?
* It doesn’t mean we have to use Collection implementations instead of arrays. We can’t store primitive types in
Collections, performance wise arrays are better. The recommendation is when we know the size in advance
then arrays is better to use, if size is not fixed and data processing required like sorting, searching then
collections implementations are better to use.
Topic 4.3.13
Array Collection
Difference
Arrays can hold both primitives as well Collections can hold only objects but
between 2
as objects. not primitive.
Array and 3
Performance point of view arrays
faster than collection
Performance point of view collections
are slower than array
Collection Arrays can hold only homogeneous
Collections can hold both
4 homogeneous and heterogeneous
elements.
elements.
Memory point of view arrays are not Memory point of view collections are
5
recommended to use. recommended to use.
For any requirement, there is no ready For every requirement ready made
6
method available. method support is available.
Topic 4.3.13
Collections (Pseudocode)
NAMES = new Collection() Task: This program inputs NAMES of students
NAME = ""
loop while NAME <> "quit" // pre-tested loop
who are leaving school early - for example to
input NAME visit the doctor. The names are collected in a
Collection list. When a student returns, tying
if NAME <> "quit" then
if NAMES.contains(NAME) then the same name again removes that name from
output NAME , " returned“ the list. At the end of the day, the secretary
NAMES.remove(NAME)
else types "quit" to end the program and see a list
output NAME , " is leaving" of all students who left but did not return.
NAMES.addItem(NAME)
end if
end if
end loop
SURNAMES.resetNext()
Output “These names starts with “S”
Loop while SURNAMES .hasNext()
Name= SURNAMES.getNext()
if Name.Substring(0,1)= “S” then
output Name
end if
End loop
Topic 4.3.13
Programming Example 48: Use of collection of objects
Topic 4.3.13
Topic 4.3.13