Wildcards and Generics Flashcards

(32 cards)

1
Q

Fill in the blank:
public static <T> void selectionSort(T[] arr, Comparator <\_\_\_\_> comp){
//code; overriding the compare() method
}</T>

A

public static <T> void selectionSort(T[] arr, Comparator<? super T> comp){}</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Fill in the blank:
public static <_____> void selectionSort(T[] arr){
//code; overriding the compareTo() method
}

A

public static <T extends Comparable<? super T» void selectionSort(T[] arr){
//code; overriding the compareTo() method
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fill in the blank:
If T is a type parameter and you write Comparator<T>, you probably actually want \_\_\_\_\_.</T>

A

If T is a type parameter and you write Comparator<T>, you probably actually want Comparator <? super T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fill in the blank:
If T is a type parameter and you write Comparator<T>, you probably actually want \_\_\_\_\_.</T>

A

If T is a type parameter and you write Comparator<T>, you probably actually want Comparator <? super T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Fill in the blank:
If T is a type parameter and you write “T extends Comparable<T>”, you probably actually want \_\_\_\_.</T>

A

If T is a type parameter and you write “T extends Comparable<T>”, you probably actually want “<T extends Comparable<? super T>>”</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Iterator<E>'s three key methods?</E>

A
  1. boolean hasNext()
  2. E next()
  3. void remove()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Collection<E>'s five key methods?</E>

A
  1. boolean add(E item)
  2. int size()
  3. boolean contains(Object obj)
  4. boolean remove(Object obj)
  5. Iterator<E> iterator()</E>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Iterator’s remove() may only be called…

A

…after at least one call to Iterator’s next().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Iterator’s remove() does what?

A

It removes the element returned by the last call to Iterator’s next().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If Iterator’s remove() is called before another call to Iterator’s next(), what happens?

A

An IllegalStateException is thrown.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

An Iterator needs these two things in order to function:

A
  1. int cursor
  2. boolean canRemove
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Iterator’s next() function does what three things?

A
  1. Returns the item at the CURSOR position in DATA
  2. Increments CURSOR
  3. Sets canRemove to true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Iterator’s hasNext() will return true if CURSOR
< ?????

A

Iterator’s hasNext() will return true if CURSOR < the Collection’s SIZE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Iterator’s remove() function does what three things?

A
  1. Shifts elements after CURSOR down by one and decrements Collection’s SIZE
  2. Decrements CURSOR
  3. Sets canRemove to false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The three main components of a node in a doubly linked list are:

A
  1. Data
  2. Pointer to previous node
  3. Pointer to next node
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

LinkedIterator keeps track of two nodes at once. They are:

A
  1. Cursor node
  2. pending node - named this because this node’s removal is pending, is the predecessor of cursor
16
Q

After a remove(), LinkedIterator’s pending node is set to:

A

null. This indicates that a remove will not be allowed until another next() is called.

17
Q

Runtime of get(i) for linked list

A

O(n) - must traverse list to find position

18
Q

Runtime of get(i) for array list

19
Q

Runtime of contains(item) for linked list

20
Q

Runtime of contains(item) for array list

21
Q

Runtime of size() for linked list

22
Q

Runtime of size() for array list

23
Q

Runtime of add(item) for linked list

A

O(1) - adds item at end

24
Runtime of add(i, item) for linked list
O(n) - we must traverse the list to find the position
25
Runtime of add(i, item) for array list
O(n) - we must shift elements in order to add in the middle of the array
26
Runtime of remove(item) for linked list
O(n)
27
Runtime of remove(item) for array list
O(n)
28
Runtime of add(item) for ListIterator for linked list
O(1)
29
Runtime of add(item) for ListIterator for array list
O(n) - must shift elements to add/remove
30
Runtime of remove(item) for ListIterator for linked list
O(1)
31
Runtime of remove(item) for ListIterator for array list
O(n)