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