在 java 中的双端队列实现中未为我的类型定义方法
Method undefined for my type in a deque implementation in java
所以我需要在Java中创建一个双端队列或双端队列。但是,当我在我的演示或驱动程序中使用双端队列实现中定义的一些方法时,我收到一条错误消息,指出给定的方法对于我的双端队列实现 class 的类型是未定义的。所以我的 toString() 和 size() 方法没有显示任何错误,但是入队和出队等其他所有方法都有错误。我意识到这一定是一个愚蠢的错误,但请帮助我理解。谢谢
这是我的驱动程序,它使用我的双端队列 class,然后是我的双端队列实现:
import java.util.ArrayDeque;
public class DequeDemo {
public static void main(String[] args) {
//create an object for the Deque class
ArrayDeque<Integer> cad = new ArrayDeque<Integer>();
/*Adding integers to the deque at rear*/
cad.enqueueRear(40);
cad.dequeueRear(22);
cad.enqueueFront(-4);
cad.dequeueFront(16);
System.out.println(cad.first());
System.out.println(cad.last());
}
}
双端队列的实现:
public class ArrayDeque<T> implements DequeADT<T> {
private final int DEFAULT_CAPACITY = 10;
private int front, rear, count;
private T[] deque;
//creates an empty deque using default capacity
public ArrayDeque() {
front = rear = count = 0;
deque = (T[]) (new Object[DEFAULT_CAPACITY]);
}
//creates an empty deque using specified capacity
public ArrayDeque (int initialCapacity) {
front = rear = count = 0;
deque = (T[])(new Object[initialCapacity]);
}
//adds an element to the rear of the deque
public void enqueueRear(T element) {
if(size() == deque.length) {
System.out.println("The deque is full.");
return;
}
deque[rear] = element;
rear = (rear + 1)%deque.length;
count++;
}
//adds an element to the front of the deque
public void enqueueFront(T element) {
if(size() == deque.length) {
System.out.println("The deque is full.");
return;
}
if (front == 0)
front = deque.length - 1;
else
front = (front -1)% deque.length;
deque[front] = element;
count++;
}
/*removes an element at the front of the deque and returns
* a reference to it*/
public T dequeueFront() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("deque");
T result = deque[front];
deque[front] = null;
front = (front + 1)%deque.length;
count--;
return result;
}
/*removes an element at the rear of the deque and returns
* a reference to it*/
public T dequeueRear() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("deque");
T result = deque[rear -1];
deque[rear -1] = null;
rear = (rear -1)% deque.length;
count--;
return result;
}
//returns an element at the front of the deque
public T first() {
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = deque[front];
return result;
}
//returns the element the deque rear
public T last() {
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = deque[rear -1];
return result;
}
//returns true if deque is empty
public boolean isEmpty() {
return (count==0);
}
//returns the number of elements in the deque
public int size() {
return count;
}
//returns a string representation of the deque
public String toString() {
String queueElements = "";
for(int i=front, j=0;j<count;i=(i+1)%deque.length, j++) {
queueElements = queueElements + deque[i].toString() + " ";
}
return queueElements;
}
}
您正在调用一个存在的方法,但方法声明中没有参数。该方法是
public T dequeueRear()
然后您尝试执行以下操作
cad.dequeueRear(22);
您必须改为执行以下操作
cad.dequeueRear();
所以我需要在Java中创建一个双端队列或双端队列。但是,当我在我的演示或驱动程序中使用双端队列实现中定义的一些方法时,我收到一条错误消息,指出给定的方法对于我的双端队列实现 class 的类型是未定义的。所以我的 toString() 和 size() 方法没有显示任何错误,但是入队和出队等其他所有方法都有错误。我意识到这一定是一个愚蠢的错误,但请帮助我理解。谢谢
这是我的驱动程序,它使用我的双端队列 class,然后是我的双端队列实现:
import java.util.ArrayDeque;
public class DequeDemo {
public static void main(String[] args) {
//create an object for the Deque class
ArrayDeque<Integer> cad = new ArrayDeque<Integer>();
/*Adding integers to the deque at rear*/
cad.enqueueRear(40);
cad.dequeueRear(22);
cad.enqueueFront(-4);
cad.dequeueFront(16);
System.out.println(cad.first());
System.out.println(cad.last());
}
}
双端队列的实现:
public class ArrayDeque<T> implements DequeADT<T> {
private final int DEFAULT_CAPACITY = 10;
private int front, rear, count;
private T[] deque;
//creates an empty deque using default capacity
public ArrayDeque() {
front = rear = count = 0;
deque = (T[]) (new Object[DEFAULT_CAPACITY]);
}
//creates an empty deque using specified capacity
public ArrayDeque (int initialCapacity) {
front = rear = count = 0;
deque = (T[])(new Object[initialCapacity]);
}
//adds an element to the rear of the deque
public void enqueueRear(T element) {
if(size() == deque.length) {
System.out.println("The deque is full.");
return;
}
deque[rear] = element;
rear = (rear + 1)%deque.length;
count++;
}
//adds an element to the front of the deque
public void enqueueFront(T element) {
if(size() == deque.length) {
System.out.println("The deque is full.");
return;
}
if (front == 0)
front = deque.length - 1;
else
front = (front -1)% deque.length;
deque[front] = element;
count++;
}
/*removes an element at the front of the deque and returns
* a reference to it*/
public T dequeueFront() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("deque");
T result = deque[front];
deque[front] = null;
front = (front + 1)%deque.length;
count--;
return result;
}
/*removes an element at the rear of the deque and returns
* a reference to it*/
public T dequeueRear() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("deque");
T result = deque[rear -1];
deque[rear -1] = null;
rear = (rear -1)% deque.length;
count--;
return result;
}
//returns an element at the front of the deque
public T first() {
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = deque[front];
return result;
}
//returns the element the deque rear
public T last() {
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = deque[rear -1];
return result;
}
//returns true if deque is empty
public boolean isEmpty() {
return (count==0);
}
//returns the number of elements in the deque
public int size() {
return count;
}
//returns a string representation of the deque
public String toString() {
String queueElements = "";
for(int i=front, j=0;j<count;i=(i+1)%deque.length, j++) {
queueElements = queueElements + deque[i].toString() + " ";
}
return queueElements;
}
}
您正在调用一个存在的方法,但方法声明中没有参数。该方法是
public T dequeueRear()
然后您尝试执行以下操作
cad.dequeueRear(22);
您必须改为执行以下操作
cad.dequeueRear();