()
| 55 | |
| 56 | // remove and return a random item |
| 57 | public Item dequeue() { |
| 58 | if (isEmpty()) throw new NoSuchElementException(); |
| 59 | int id = (first + StdRandom.uniformInt(n)) % v.length; |
| 60 | Item item = v[id]; |
| 61 | v[id] = v[first]; |
| 62 | v[first++] = null; |
| 63 | n--; |
| 64 | if (first == v.length) first = 0; |
| 65 | if (n > 0 && n == v.length / 4) resize(v.length / 2); |
| 66 | return item; |
| 67 | } |
| 68 | |
| 69 | // return a random item (but do not remove it) |
| 70 | public Item sample() { |