我有一个家庭作业,这是我在这里的第一个问题,并且已经在这里工作了一段时间。通常我可以解决问题,但我真的被卡住了。这是我的代码和使用测试器的输出。我的moveToBack是一种来自enqueue方法的复制粘贴。我还没弄清楚为什么输出很糟糕。它甚至不像一个入队行为!
import java.util.*;
public class NoDuplicatesArrayQueue<T> implements
NoDuplicatesQueueInterface<T>, java.io.Serializable {
private T[] queue;
private int frontIndex;
private int backIndex;
private static final int INITIAL_CAPACITY = 50;
private int length = 0;
public NoDuplicatesArrayQueue() {
this(INITIAL_CAPACITY);
}// end NoDuplicatesArrayQueue
public NoDuplicatesArrayQueue(int initialCapacity) {
queue = (T[]) new Object[initialCapacity + 1];
frontIndex = 0;
backIndex = initialCapacity;
}// end NoDuplicatesArrayQueue
public void display() {
for (int i = 0; i < length; i++)
System.out.println(queue[i]);
}// end display
public T dequeue() {
T front = null;
if (!isEmpty()) {
front = queue[frontIndex];
frontIndex = (frontIndex + 1) % queue.length;
} // end if
length--;
return front;
} // end dequeue
public void enqueue(T newEntry) {
boolean found = false;
for (int index = 0; !found && (index < length); index++) {
if (newEntry.equals(queue[index]))
found = true;
}// end for
if (found == false) {
if (isArrayFull())
doubleArray();
backIndex = (backIndex + 1) % queue.length;
queue[backIndex] = newEntry;
// System.out.println(length);
length++;
} else {
System.out.println("A duplicate exists");
}//end else
}//end enqueue
public T getFront() {
T front = null;
if (!isEmpty())
front = queue[frontIndex];
return front;
} // end getFront
public boolean isEmpty() {
return frontIndex == ((backIndex + 1) % queue.length);
} // end isEmpty
public void clear() {
if (!isEmpty()) { // deallocates only the used portion
for (int index = frontIndex; index != backIndex; index = (index + 1)
% queue.length) {
queue[index] = null;
} // end for
queue[backIndex] = null;
} // end if
frontIndex = 0;
backIndex = queue.length - 1;
} // end clear
public void moveToBack(T newEntry) {
boolean found = false;
for (int index = 0; !found && (index < length); index++) {
if (newEntry.equals(queue[index]))
found = true;
}//end for
if (found == false) {
// if (isArrayFull())
// doubleArray();
backIndex = (backIndex + 1) % queue.length;
queue[backIndex] = newEntry;
System.out.println(newEntry);
length++;
} else {
System.out.println("A duplicate exists");
}//end else
}//end moveToBack
private boolean isArrayFull() {
return frontIndex == ((backIndex + 2) % queue.length);
} // end isArrayFull
private void doubleArray() {
T[] oldQueue = queue;
int oldSize = oldQueue.length;
queue = (T[]) new Object[2 * oldSize];
for (int index = 0; index < oldSize - 1; index++) {
queue[index] = oldQueue[frontIndex];
frontIndex = (frontIndex + 1) % oldSize;
} // end for
frontIndex = 0;
backIndex = oldSize - 2;
} // end doubleArray
}
OUTPUT TESTER
public class LabDPartBDriver {
public static void main(String args[])
{
NoDuplicatesArrayQueue<Integer> test1 = new NoDuplicatesArrayQueue<Integer>();
test1.enqueue(1);
test1.enqueue(3);
test1.enqueue(2);
test1.enqueue(0);
test1.enqueue(-1);
System.out.println("The queue has ");
test1.display();
System.out.println();
test1.enqueue(test1.dequeue());
test1.enqueue(test1.dequeue());
test1.enqueue(test1.dequeue());
test1.enqueue(test1.dequeue());
test1.enqueue(test1.dequeue());
System.out.println("The queue should be the same ");
test1.display();
System.out.println();
test1.enqueue(1);
test1.enqueue(3);
test1.enqueue(2);
test1.enqueue(0);
test1.enqueue(-1);
System.out.println("The queue should be the same ");
test1.display();
System.out.println();
test1.moveToBack(3);
System.out.println("The queue should be 1, 2, 0, -1, 3 ");
test1.display();
System.out.println();
test1.moveToBack(0);
System.out.println("The queue should be 1, 2, -1, 3, 0 ");
test1.display();
System.out.println();
test1.moveToBack(1);
System.out.println("The queue should be 2, -1, 3, 0, 1");
test1.display();
System.out.println();
test1.moveToBack(5);
test1.moveToBack(5);
test1.moveToBack(5);
test1.moveToBack(5);
test1.moveToBack(5);
test1.moveToBack(5);
System.out.println("The queue should be 2, -1, 3, 0, 1, 5");
test1.display();
System.out.println();
}
}
OUTPUT .. 队列有 1 3 2 0 -1
存在重复 存在重复 队列应该是相同的 1 3 2
存在重复 存在重复 存在重复 队列应该是相同的 1 3 2 0 -1
存在重复 队列应该是1,2,0,-1,3 1 3 2 0 -1
存在重复 队列应该是1,2,-1,3,0 1 3 2 0 -1
存在重复 队列应为2,-1,3,0,1 1 3 2 0 -1
5 五 五 五 五 五 队列应为2,-1,3,0,1,5 1 3 2 0 -1 2 0 -1 0 -1 5
答案 0 :(得分:0)
它说它是重复的,因为当你出列它时你没有从数组中删除值..你只是获取值并返回它..
详细解释一下这个
| 1 | 3 | 2 | 0 | -1 |
^(front pointer moved after deque)
在您正在从index = 0,
搜索的for循环中进行排队并且在队列[0]中存在值1,并且您正在对被激活的1
(它没有真正出列)进行排队,这会导致重复的条目。
我还建议您考虑使用ArrayList
而不是数组,并使用Iterator
来迭代列表并在出列时删除。