Wildcards and Generics Flashcards
Fill in the blank:
public static <T> void selectionSort(T[] arr, Comparator <\_\_\_\_> comp){
//code; overriding the compare() method
}</T>
public static <T> void selectionSort(T[] arr, Comparator<? super T> comp){}</T>
Fill in the blank:
public static <_____> void selectionSort(T[] arr){
//code; overriding the compareTo() method
}
public static <T extends Comparable<? super T» void selectionSort(T[] arr){
//code; overriding the compareTo() method
}
Fill in the blank:
If T is a type parameter and you write Comparator<T>, you probably actually want \_\_\_\_\_.</T>
If T is a type parameter and you write Comparator<T>, you probably actually want Comparator <? super T></T>
Fill in the blank:
If T is a type parameter and you write Comparator<T>, you probably actually want \_\_\_\_\_.</T>
If T is a type parameter and you write Comparator<T>, you probably actually want Comparator <? super T></T>
Fill in the blank:
If T is a type parameter and you write “T extends Comparable<T>”, you probably actually want \_\_\_\_.</T>
If T is a type parameter and you write “T extends Comparable<T>”, you probably actually want “<T extends Comparable<? super T>>”</T>
What are Iterator<E>'s three key methods?</E>
- boolean hasNext()
- E next()
- void remove()
What are Collection<E>'s five key methods?</E>
- boolean add(E item)
- int size()
- boolean contains(Object obj)
- boolean remove(Object obj)
- Iterator<E> iterator()</E>
Iterator’s remove() may only be called…
…after at least one call to Iterator’s next().
Iterator’s remove() does what?
It removes the element returned by the last call to Iterator’s next().
If Iterator’s remove() is called before another call to Iterator’s next(), what happens?
An IllegalStateException is thrown.
An Iterator needs these two things in order to function:
- int cursor
- boolean canRemove
Iterator’s next() function does what three things?
- Returns the item at the CURSOR position in DATA
- Increments CURSOR
- Sets canRemove to true
Iterator’s hasNext() will return true if CURSOR
< ?????
Iterator’s hasNext() will return true if CURSOR < the Collection’s SIZE.
Iterator’s remove() function does what three things?
- Shifts elements after CURSOR down by one and decrements Collection’s SIZE
- Decrements CURSOR
- Sets canRemove to false
The three main components of a node in a doubly linked list are:
- Data
- Pointer to previous node
- Pointer to next node
LinkedIterator keeps track of two nodes at once. They are:
- Cursor node
- pending node - named this because this node’s removal is pending, is the predecessor of cursor
After a remove(), LinkedIterator’s pending node is set to:
null. This indicates that a remove will not be allowed until another next() is called.
Runtime of get(i) for linked list
O(n) - must traverse list to find position
Runtime of get(i) for array list
O(1)
Runtime of contains(item) for linked list
O(n)
Runtime of contains(item) for array list
O(n)
Runtime of size() for linked list
O(1)
Runtime of size() for array list
O(1)
Runtime of add(item) for linked list
O(1) - adds item at end
Runtime of add(i, item) for linked list
O(n) - we must traverse the list to find the position
Runtime of add(i, item) for array list
O(n) - we must shift elements in order to add in the middle of the array
Runtime of remove(item) for linked list
O(n)
Runtime of remove(item) for array list
O(n)
Runtime of add(item) for ListIterator for linked list
O(1)
Runtime of add(item) for ListIterator for array list
O(n) - must shift elements to add/remove
Runtime of remove(item) for ListIterator for linked list
O(1)
Runtime of remove(item) for ListIterator for array list
O(n)