【发布时间】:2014-06-27 07:09:21
【问题描述】:
public static void main(String args[])
{
// Build a queue containing the Integers 1,2,...,6:
RandomizedQueue<Integer> Q= new RandomizedQueue<Integer>();
for (int i = 1; i < 7; ++i) Q.enqueue(i); // autoboxing! cool!
// Print 30 die rolls to standard output
StdOut.print("Some die rolls: ");
for (int i = 1; i < 30; ++i) StdOut.print(Q.sample() +" ");
StdOut.println();
// Let's be more serious: do they really behave like die rolls?
int[] rolls= new int [10000];
for (int i = 0; i < 10000; ++i)
rolls[i] = Q.sample(); // autounboxing! Also cool!
StdOut.printf("Mean (should be around 3.5): %5.4f\n", StdStats.mean(rolls));
StdOut.printf("Standard deviation (should be around 1.7): %5.4f\n",
StdStats.stddev(rolls));
// Let's look at the iterator. First, we make a queue of colours:
RandomizedQueue<String> C= new RandomizedQueue<String>();
C.enqueue("red"); C.enqueue("blue"); C.enqueue("green"); C.enqueue("yellow");
Iterator I= C.iterator();
Iterator J= C.iterator();
Iterator K= C.iterator();
Iterator L= C.iterator();
Iterator M= C.iterator();
Iterator N= C.iterator();
StdOut.print("Two colours from first shuffle: ");
StdOut.print(I.next()+" ");
StdOut.print(I.next()+" ");
StdOut.print("\nEntire second shuffle: ");
while (J.hasNext()) StdOut.print(J.next()+" ");
StdOut.print("\nEntire third shuffle: ");
while (K.hasNext()) StdOut.print(K.next()+" ");
StdOut.print("\nEntire fourth shuffle: ");
while (L.hasNext()) StdOut.print(L.next()+" ");
StdOut.print("\nEntire fifth shuffle: ");
while (M.hasNext()) StdOut.print(M.next()+" ");
StdOut.print("\nEntire sixth shuffle: ");
while (N.hasNext()) StdOut.print(N.next()+" ");
StdOut.print("\nRemaining two colours from first shuffle: ");
StdOut.print(I.next()+" ");
StdOut.println(I.next());
}
我从某个地方借了这个客户端来检查我的 RandomizedQueue 实现。其他一切正常,但它的迭代器部分不起作用。我的意思是一次运行的示例输出是:
Some die rolls: 1 1 5 6 3 6 6 1 2 4 6 2 4 5 6 4 4 2 2 5 6 6 2 6 4 5 3 3 2
Mean (should be around 3.5): 3.4882
Standard deviation (should be around 1.7): 1.7069
Two colours from first shuffle: yellow green
Entire second shuffle: yellow green blue red
Entire third shuffle: yellow green blue red
Entire fourth shuffle: yellow green blue red
Entire fifth shuffle: yellow green blue red
Entire sixth shuffle: yellow green blue red
Remaining two colours from first shuffle: blue red
特定迭代器的迭代应该在 RandomizedQueue 中的对象上是随机的,并且每个迭代器都应该记住它自己的特定顺序(一旦声明)。 这是我的 Iterable 和 Iterator 代码:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item> {
private Item[] a;
private int N;
public RandomizedQueue() {
a = (Item[]) new Object[2];
} // construct an empty randomized queue
public boolean isEmpty() {
return N == 0;
} // is the queue empty?
public int size() {
return N;
} // return the number of items on the queue
public void enqueue(Item item) {
if (item == null) throw new NullPointerException();
if (N == a.length) resize(2*a.length); // double size of array if necessary
a[N++] = item;
} // add the item
private void resize(int capacity) {
assert capacity >= N;
Item[] temp = (Item[]) new Object[capacity];
for(int i = 0; i < N; i++) {
temp[i] = a[i];
}
a = temp;
}
public Item dequeue() {
if(isEmpty()) throw new java.util.NoSuchElementException();
int random = StdRandom.uniform(N); // generating a number b/w 0 and N-1
// swap with the final element
Item temp = a[random];
a[random] = a[N-1];
a[N-1] = temp;
Item item = a[N-1];
a[N-1] = null;
N--;
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
} // delete and return a random item
public Item sample() {
if(isEmpty()) throw new java.util.NoSuchElementException();
int random = StdRandom.uniform(N); // generating a number b/w 0 and N-1
Item item = a[random];
return item;
} // return (but do not delete) a random item
public Iterator<Item> iterator() {
return new RandomIterator();
} // return an independent iterator over items in random order
private class RandomIterator implements Iterator<Item> {
private int i = N;
private Item[] itemcopy = (Item[]) new Object[N];
public RandomIterator() {
System.arraycopy(a, 0, itemcopy, 0, N);
StdRandom.shuffle(itemcopy, 0, N-1);
}
public boolean hasNext() { return i > 0 ;}
public void remove() { throw new UnsupportedOperationException(); }
public Item next()
{
if(!hasNext()) throw new NoSuchElementException();
return a[--i];
}
}
}
StdIn、StdOut 和 StdRandom 是为赋值提供的类,它们的功能已在程序中使用,它们的工作方式与它们的名字所暗示的一样。
那么为什么不同的迭代器不产生随机输出。 shuffle 产生对象的随机排列。 arraycopy 将一个数组复制到另一个数组。 StdOut.print 的行为类似于 System.out.print
【问题讨论】:
-
RandomizedQueue 迭代器是否返回 RandomIterator 的实例?在 RandomIterator 中你在哪里传递
N。看起来您使用值初始化了 RandomIterator,但是,如果您在初始化后入队,则没有任何内容可以开始,因此 nothing 将被打乱 -
@blueygh2 我应该发布完整的 RandomizedQueue 类吗?
-
这可能会有所帮助
-
@bluegh2 我已经发布了完整的课程
-
@blueygh2 我发现它是一个阴险的错误。在
next()方法中,它应该是itemcopy[--i]而不是a[--i]