Week 5 - Collections Flashcards
Static data structures…
…are collections of data of a fixed size when a program is compiled.
Dynamic data structures are…
…variable in size & structure (ie an arraylist)
Collections are primarily defined through…
A set of interfaces
What collection cannot contain duplicate elements?
A Set
What collection does allow duplicate elements?
Lists
Data structures store and access data in different ways? True or false?
True
What do ArrayLists() store?
Object references
They differ from arrays as arrays store primitive data
An ArrayList() has a fixed number of items? True or false?
False.
Normal Arrays have fixed items declared at the beginning. ArrayLists can automatically increase.
The size of an arraylist object is the current number of items stored
After importing the Java.util.ArrayList package how are ArrayLists declared?
ArrayList myArray = new ArrayList();
myArray.add(“San”);
myArray.add(“Saint”);
Only objects can be added to a collection. True or false?
True.
public void add (Object element) - the signature for the ArrayList add method
To add primitive you need a wrapper class:
myList.add(new Integer(5)); // wrap
Integer num = (Integer)myList.get(0); // unwrap
Int n = num.intValue();
Which sets implement the Set interface?
TreeSet (binary tree, stores in order) and HashSet (no ordering of elements)
A hash map implements what interface
The map interface
True or false, a hashmap is a collection of key-value pairs?
True.
For example a phone directory “John Smith”, “0162347272”.
Each entry has a name-number association
What if statement would a hashmap use to check if a hashmap contained “Andrew”
if(.containsKey(“Andrew”
How do we add a value to an ArrayList?
ArrayList insects = new ArrayList();
insects.add(“horsefly”)