Collections: Difference between revisions
Mr. MacKenty (talk | contribs) (Created page with "right|frame|Programming basics<ref>http://www.flaticon.com/</ref> A collection — sometimes called a container — is simply an object that groups multip...") |
Mr. MacKenty (talk | contribs) No edit summary |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[file:arrows.png|right|frame|Programming basics<ref>http://www.flaticon.com/</ref>]] | [[file:arrows.png|right|frame|Programming basics<ref>http://www.flaticon.com/</ref>]] | ||
A collection | A collection is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language — or just about any other programming language — you are already familiar with collections.<ref>https://docs.oracle.com/javase/tutorial/collections/intro/</ref> | ||
== Collection methods == | |||
'''Collection methods in Pseudocode are:'''<ref>http://ib.compscihub.net/wp-content/uploads/2015/04/4.3.10.pdf</ref> | |||
* .addItem( new data item ) | |||
* .resetNext( ) start at beginning of collection | |||
* .hasNext( ) checks whether there are still more items in the collection | |||
* .getNext( ) retrieve the next item in the collection | |||
* .isEmpty( ) check whether the collection is empty | |||
== Code sample == | |||
<syntaxhighlight lang="Python"> | |||
class CustomCollection: | |||
def __init__(self): | |||
self.items = [] | |||
self.current_index = 0 | |||
def add_item(self, item): | |||
self.items.append(item) | |||
def reset_next(self): | |||
self.current_index = 0 | |||
def has_next(self): | |||
return self.current_index < len(self.items) | |||
def get_next(self): | |||
if self.has_next(): | |||
item = self.items[self.current_index] | |||
self.current_index += 1 | |||
return item | |||
else: | |||
raise IndexError("No more items in the collection.") | |||
def is_empty(self): | |||
return len(self.items) == 0 | |||
def __repr__(self): | |||
return f"CustomCollection({self.items})" | |||
# Create a new custom collection | |||
collection = CustomCollection() | |||
# Add items to the collection | |||
collection.add_item(1) | |||
collection.add_item(2) | |||
collection.add_item(3) | |||
# Check if the collection is empty | |||
print(collection.is_empty()) # Output: False | |||
# Iterate through the collection using hasNext and getNext methods | |||
while collection.has_next(): | |||
print(collection.get_next()) # Output: 1, 2, 3 | |||
# Check if there are more items in the collection | |||
print(collection.has_next()) # Output: False | |||
# Reset the iterator to the beginning of the collection | |||
collection.reset_next() | |||
# Iterate through the collection again | |||
while collection.has_next(): | |||
print(collection.get_next()) # Output: 1, 2, 3 | |||
# Clear the collection | |||
collection.items.clear() | |||
# Check if the collection is empty | |||
print(collection.is_empty()) # Output: True | |||
</syntaxhighlight> | |||
Latest revision as of 06:44, 26 April 2023
A collection is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language — or just about any other programming language — you are already familiar with collections.[2]
Collection methods[edit]
Collection methods in Pseudocode are:[3]
- .addItem( new data item )
- .resetNext( ) start at beginning of collection
- .hasNext( ) checks whether there are still more items in the collection
- .getNext( ) retrieve the next item in the collection
- .isEmpty( ) check whether the collection is empty
Code sample[edit]
class CustomCollection:
def __init__(self):
self.items = []
self.current_index = 0
def add_item(self, item):
self.items.append(item)
def reset_next(self):
self.current_index = 0
def has_next(self):
return self.current_index < len(self.items)
def get_next(self):
if self.has_next():
item = self.items[self.current_index]
self.current_index += 1
return item
else:
raise IndexError("No more items in the collection.")
def is_empty(self):
return len(self.items) == 0
def __repr__(self):
return f"CustomCollection({self.items})"
# Create a new custom collection
collection = CustomCollection()
# Add items to the collection
collection.add_item(1)
collection.add_item(2)
collection.add_item(3)
# Check if the collection is empty
print(collection.is_empty()) # Output: False
# Iterate through the collection using hasNext and getNext methods
while collection.has_next():
print(collection.get_next()) # Output: 1, 2, 3
# Check if there are more items in the collection
print(collection.has_next()) # Output: False
# Reset the iterator to the beginning of the collection
collection.reset_next()
# Iterate through the collection again
while collection.has_next():
print(collection.get_next()) # Output: 1, 2, 3
# Clear the collection
collection.items.clear()
# Check if the collection is empty
print(collection.is_empty()) # Output: True
Standards[edit]
- Describe the characteristics and applications of a collection.
- Construct algorithms using the access methods of a collection.
- Discuss the need for sub-programmes and collections within programmed solutions.
- Construct algorithms using pre-defined sub-programmes, one-dimensional arrays and/or collections.