因此,在过去的几个小时中,我查看了此代码,但未找到错误。子集接收一个由3个String字符组成的数组,它将仔细读取并打印出来,但是,如果我添加了三个以上的元素,它将抛出arrayoutofbounds异常,我无法弄清楚原因。我可以一一手动添加它们,它可以工作,但是那不是程序设计工作的方式。我确定解决方案很简单,但我找不到它。有任何想法吗?非常感谢。
package a02;
import java.util.Iterator;
import edu.princeton.cs.algs4.StdRandom;
/**
*
* @author Danny
*
* @param <Item>
*/
public class RandomizedQueue<Item> implements Iterable<Item> {
private Item[] queue ;
private int size;
/**
* Constructs an empty randomized queue.
*/
public RandomizedQueue() {
queue = (Item[])new Object[1];
size=0;
}
/**
* parameterized constructor for the Iterator class.
* @param array
*/
private RandomizedQueue(Item[] array)
{
int count = 0;
queue = array;
while(queue[count]!=null)
count++;
queue = (Item[])new Object[array.length];
for(int i=0;i<count;i++)
queue[i]=array[i];
size=count;
}
private Item[] getArray() {
return queue;
}
/**
* Checks to see if the queue is empty.
* @return
*/
public boolean isEmpty() {
return size<=0;
}
/**
* Returns the number of items in the queue.
* @return
*/
public int size() {
return size;
}
/**
* Adds an item.
* @param item
*/
public void enqueue(Item item) {
if (item == null)
throw new java.lang.NullPointerException();
if (size == queue.length)
resize(2*queue.length);
queue[size]= item;
size++;
}
/**
* Deletes and returns a random item
* @return
*/
public Item dequeue() {
if (isEmpty())
throw new java.util.NoSuchElementException();
int index = StdRandom.uniform(size);
Item item = queue[index];
if(index == size-1)
queue[index] = null;
else {
queue[index] = queue[size-1];
queue[size-1]=null;
}
size--;
if(size<=queue.length/4)
resize(queue.length/2);
return item;
}
/**
* Returns a random item, but does not delete it.
* @return
*/
public Item sample() {
if (isEmpty())
throw new java.util.NoSuchElementException();
int index = StdRandom.uniform(size);
return queue[index];
}
/**
* Returns an independent iterator over items in random order.
*/
public Iterator<Item> iterator(){
return new RandomizedQueueIterator();
}
/**
*
* @author Danny
*
*/
private class RandomizedQueueIterator implements Iterator<Item>{
RandomizedQueue<Item> rq = new RandomizedQueue<>(queue);
@Override
public boolean hasNext() {
return rq.size()!=0;
}
@Override
public Item next() {
return rq.dequeue();
}
@Override
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
}
/**
* Resizes the queue array by the given size.
* @param d
*/
private void resize(int d) {
Item[] newArray = (Item[]) new Object[d];
for(int i = 0;i<size;i++)
newArray[i]=queue[i];
queue = newArray;
}
/**
* Unit Testing
* @param args
*/
public static void main(String[] args) {
RandomizedQueue<Integer> rq = new RandomizedQueue<>();
rq.enqueue(1);
rq.enqueue(2);
rq.enqueue(3);
rq.enqueue(4);
rq.enqueue(5);
rq.enqueue(6);
rq.enqueue(7);
Iterator<Integer> iterator1 = rq.iterator();
while(iterator1.hasNext())
System.out.println(iterator1.next());
System.out.println();
Iterator<Integer> iterator2 = rq.iterator();
while(iterator2.hasNext())
System.out.println(iterator2.next());
}
}
package a02;
import java.util.Iterator;
public class Subset {
public static void main(String[] args) {
//char [] c = {'b', 'a', 'C'};
String[] c= {"hello", "hi", "howdy", "english"};
RandomizedQueue<String> queue = new RandomizedQueue<String>();
for (int i=0;i<c.length; i++)
queue.enqueue(c[i]);
int k=3;
Iterator<String> iterator=queue.iterator();
for (int i=0; i<k;i++) {
System.out.println(iterator.next());
}
}
}
答案 0 :(得分:0)
在构造器RandomizedQueue(Item[] array)
中,您需要在访问queue
之前添加边界检查。
更改:
while(queue[count]!=null)
到
while(count < queue.length && queue[count]!=null)