在java中的deque实现中未定义my type的方法
因此,我需要在Java中创建一个deque或双端队列。但是,当我在演示或驱动程序中使用deque实现中定义的一些方法时,我得到一个错误,即给定的方法对于deque实现类的类型是未定义的。因此,我的toString()和size()方法没有显示任何错误,但其他所有方法(如排队和出列)都有错误。我意识到这一定是一个愚蠢的错误,但请帮助我理解。谢谢 下面是使用deque类的驱动程序,然后是deque的实现:在java中的deque实现中未定义my type的方法,java,linked-list,queue,deque,arraydeque,Java,Linked List,Queue,Deque,Arraydeque,因此,我需要在Java中创建一个deque或双端队列。但是,当我在演示或驱动程序中使用deque实现中定义的一些方法时,我得到一个错误,即给定的方法对于deque实现类的类型是未定义的。因此,我的toString()和size()方法没有显示任何错误,但其他所有方法(如排队和出列)都有错误。我意识到这一定是一个愚蠢的错误,但请帮助我理解。谢谢 下面是使用deque类的驱动程序,然后是deque的实现: import java.util.ArrayDeque; public class Dequ
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;
}
}
import java.util.ArrayDeque;
公共类DequeDemo{
公共静态void main(字符串[]args){
//为Deque类创建一个对象
ArrayDeque cad=新的ArrayDeque();
/*将整数添加到后面的三角形中*/
后置计算机辅助设计(40);
cad.dequeuer(22);
计算机辅助设计排队前(-4);
计算机辅助设计(16);
System.out.println(cad.first());
System.out.println(cad.last());
}
}
deque的实施:
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类ArrayDeque实现了DequeADT{
专用最终int默认_容量=10;
前,后,计数;
私人T[]德克;
//使用默认容量创建空数据块
公共ArrayDeque(){
前=后=计数=0;
deque=(T[])(新对象[默认容量];
}
//使用指定的容量创建空数据块
公共阵列(初始容量){
前=后=计数=0;
deque=(T[])(新对象[initialCapacity]);
}
//将元素添加到三角形的后部
公共无效排队(T元素){
if(size()==deque.length){
System.out.println(“数据已满”);
回来
}
deque[后部]=元件;
后=(后+1)%deque.length;
计数++;
}
//将元素添加到三角形的前面
公共无效队列前端(T元素){
if(size()==deque.length){
System.out.println(“数据已满”);
回来
}
如果(前==0)
前端=端部长度-1;
其他的
前=(前-1)%deque.length;
deque[前]=元素;
计数++;
}
/*删除deque前面的元素并返回
*对它的引用*/
public T dequeueFront()引发EmptyCollectionException异常
{
if(isEmpty())
抛出新的EmptyCollectionException(“deque”);
T结果=德克[前];
deque[前端]=空;
前=(前+1)%deque.length;
计数--;
返回结果;
}
/*移除deque后面的元素并返回
*对它的引用*/
public T dequeueRear()引发EmptyCollectionException异常{
if(isEmpty())
抛出新的EmptyCollectionException(“deque”);
T结果=deque[后-1];
deque[后-1]=空;
后=(后-1)%deque.length;
计数--;
返回结果;
}
//返回deque前面的元素
公共交通优先{
if(isEmpty())
抛出新的EmptyCollectionException(“队列”);
T结果=德克[前];
返回结果;
}
//返回deque rear的元素
公共交通最后一站{
if(isEmpty())
抛出新的EmptyCollectionException(“队列”);
T结果=deque[后-1];
返回结果;
}
//如果deque为空,则返回true
公共布尔值为空(){
返回(计数=0);
}
//返回数据块中的元素数
公共整数大小(){
返回计数;
}
//返回deque的字符串表示形式
公共字符串toString(){
字符串queueElements=“”;
对于(inti=front,j=0;j您正在调用一个存在的方法,但该方法声明中没有参数。该方法是
public T dequeueRear()
你试着做下面的事情
cad.dequeueRear(22);
您必须执行以下操作
cad.dequeueRear();
删除import java.util.ArrayDeque;
-您不是在测试您的类ArrayDeque
,而是在@ElliottFrisch所提到的基础上,在您的主方法中,当您调用dequeueRear
时,您试图向它传递一个参数,而方法的签名s-->dequeueRear()
@ElliottFrisch真管用!非常感谢:)@Aominè你说得对,真不敢相信我没有注意到。非常感谢!!