Java 得到了一个奇怪的NoSuchElementException
我们正试图编译我们的程序,但我们不断得到一个Java 得到了一个奇怪的NoSuchElementException,java,algorithm,exception,queue,nosuchelementexception,Java,Algorithm,Exception,Queue,Nosuchelementexception,我们正试图编译我们的程序,但我们不断得到一个无接触元素异常。有人知道为什么会这样吗?提前谢谢。在下面,我将附上实现异常的代码和main方法 编辑-以下格式中的整个代码: import java.util.Iterator; import edu.princeton.cs.algs4.*; public class RandomQueue<Item> implements Iterable<Item> { private Item[] queue; pri
无接触元素异常
。有人知道为什么会这样吗?提前谢谢。在下面,我将附上实现异常的代码和main方法
编辑-以下格式中的整个代码:
import java.util.Iterator;
import edu.princeton.cs.algs4.*;
public class RandomQueue<Item> implements Iterable<Item> {
private Item[] queue;
private int N;
private int size;
// Your code goes here.
public RandomQueue() { // create an empty random queue
N = 0;
size = 2;
queue = (Item[]) new Object[size];
}
public boolean isEmpty() {// is it empty?
if(N == 0) {
return true;
} else {
return false;
}
}
public int size() {// return the number of elements
return size;
}
public void resizeArray() {
if(3/4*size < N) {
size = size*2;
Item[] queueUpdated = (Item[]) new Object[size];
for(int i = 0; i < queue.length; ++i) {
queueUpdated[i] = queue[i];
}
queue = queueUpdated;
} else if (N < 1/4*size) {
size = size/2;
Item[] queueUpdated = (Item[]) new Object[size];
for(int i = 0; i < size-1; ++i) {
queueUpdated[i] = queue[i];
}
queue = queueUpdated;
}
}
public void enqueue(Item item) {// add an item
if(N < queue.length) {
queue[N++] = item;
resizeArray();
}
}
public Item sample(){ // return (but do not remove) a random item
if(isEmpty()) {
throw new RuntimeException("No such elements");
} else {
return queue[StdRandom.uniform(N)];
}
}
public Item dequeue(){ // remove and return a random item
if(isEmpty()) {
throw new RuntimeException("Queue is empty");
} else {
System.out.println(N);
int indexFraArray = StdRandom.uniform(N);
Item i = queue[indexFraArray];
queue[N] = null;
queue[indexFraArray] = queue[N--];
resizeArray();
return i;
}
}
private class RandomQueueIterator<E> implements Iterator<E> {
int i = 0;
public boolean hasNext() {
return i < N;
}
public E next() {
if (!hasNext()) {
throw new java.util.NoSuchElementException(); // line 88
}
i++;
return (E) dequeue();
}
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
}
public Iterator<Item> iterator() { // return an iterator over the items in
random order
return new RandomQueueIterator();
}
// The main method below tests your implementation. Do not change it.
public static void main(String args[]) {
// Build a queue containing the Integers 1,2,...,6:
RandomQueue<Integer> Q = new RandomQueue<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));
// Now remove 3 random values
StdOut.printf("Removing %d %d %d\n", Q.dequeue(), Q.dequeue(), Q.dequeue());
// Add 7,8,9
for (int i = 7; i < 10; ++i) Q.enqueue(i);
// Empty the queue in random order
while (!Q.isEmpty()) StdOut.print(Q.dequeue() +" ");
StdOut.println();
// Let's look at the iterator. First, we make a queue of colours:
RandomQueue<String> C= new RandomQueue<String>();
C.enqueue("red"); C.enqueue("blue"); C.enqueue("green");
C.enqueue("yellow");
Iterator<String> I = C.iterator();
Iterator<String> J = C.iterator();
StdOut.print("Two colours from first shuffle: "+I.next()+" "+I.next()+" ");
StdOut.print("\nEntire second shuffle: ");
while (J.hasNext()) StdOut.print(J.next()+" ");
StdOut.println("\nRemaining two colours from first shuffle: "+I.next()+" "+I.next()); // line 142
}
}
import java.util.Iterator;
导入教育,普林斯顿,cs,algs4。*;
公共类RandomQueue实现了Iterable{
私有项[]队列;
私人int N;
私有整数大小;
//你的密码在这里。
public RandomQueue(){//创建一个空的随机队列
N=0;
尺寸=2;
队列=(项目[])新对象[大小];
}
公共布尔值isEmpty(){//是否为空?
如果(N==0){
返回true;
}否则{
返回false;
}
}
public int size(){//返回元素数
返回大小;
}
public void resizeArray(){
如果(3/4*尺寸
错误发生在这里:
在这里:
迭代器正在修改集合。这至少是不标准的,而且似乎让你自己感到困惑 您正在队列
C
上创建两个迭代器,此时队列中有4个元素:
Iterator<String> I = C.iterator();
Iterator<String> J = C.iterator();
这将通过此行删除(出列)这两个元素:
return (E) dequeue();
现在,队列中有2个元素<代码>N为2
您可以尝试在此处删除其余2个元素:
StdOut.print("\nEntire second shuffle: ");
while (J.hasNext()) StdOut.print(J.next()+" ");
但是,在删除一个元素后,J.i
为1,N
为1,因此迭代器J
认为队列已耗尽,只给出这一个元素。还有一个<代码>N为1。但您试图删除另外两个元素:
StdOut.print("Two colours from first shuffle: "+I.next()+" "+I.next()+" ");
StdOut.println("\nRemaining two colours from first shuffle: "+I.next()+" "+I.next()); // line 142
这注定要失败。幸运的是它做到了next
调用hasNext
,然后比较:
return i < N;
返回i
I.I
是2(因为我们之前从I
中提取了2个元素),N
是1,所以hasNext
返回false,这会导致next
抛出异常
解决方案很简单,可能也不那么简单:迭代器不应该从您身上删除任何元素