2

我正在尝试以数组作为底层结构来实现一个自行创建的通用接口“BoundedQueue”。当我编译部分完成的类“BoundedQueueArray”时,出现错误:

3 errors found:
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java  [line: 11]
Error: csc143.data_structures.BoundedQueueArray is not abstract and does not override abstract method insert(java.lang.Object) in csc143.data_structures.BoundedQueue
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java  [line: 20]
Error: generic array creation
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java  [line: 32]
Error: name clash: insert(T) in csc143.data_structures.BoundedQueueArray and insert(T) in csc143.data_structures.BoundedQueue have the same erasure, yet neither overrides the other

这是课程:

package csc143.data_structures;

public class BoundedQueueArray<T> implements BoundedQueue {

  // elements stored in array
  private T[] elements;
  // the number of elements currently in the queue
  private int numElems;

  public BoundedQueueArray(int capacity) {
    // instantiate and bind to reference 
    elements = new T[capacity];

    numElems = 0;
  }

  /**
   * This method inserts the specified element, unless the
   * queue is full.
   * 
   * @param o The element to be inserted.
   * @throws FullQueueException If the queue is full.
   */
  public void insert(T o) throws FullQueueException {
    if(numElems < elements.length) {
     elements[numElems] = o;
     numElems++;
    } else {  // queue is full, cannot add element
      throw new FullQueueException("Queue is full.");
    }

  }

  /**
   * This method returns the element at the front of the
   * queue, unless the queue is empty.
   *
   * @return The element at the front of the queue. 
   * @throws EmptyQueueException If the queue is empty.
   */
  public T front() throws EmptyQueueException {

  }

  /**
   * This method retrieves and removes the element at the front
   * of the queue, unless the queue is empty.
   * 
   * @return The element at the front of the queue.
   * @throws EmptyQueueException If the queue is empty.
   */
  public T remove() throws EmptyQueueException {
    if(length() == 0) {
      throw new EmptyQueueException("Queue is empty.");
    }

  }

  /**
   * This method reports whether or not the queue contains
   * element(s).
   * 
   * @return If one or more element exists or not.
   */
  public boolean hasMember() {
    return length() > 0;
  }

  /**
   * This method reports whether the queue has space to add
   * element(s).
   * 
   * @return If space exists or not.
   */
  public boolean hasSpace() {
    return elements.length - length() > 0;
  }

  /**
   * This method returns the capacity of the queue.
   * 
   * @return The capacity of the queue.
   */
  public int capacity() {
    return elements.length;
  }

  /**
   * This method returns the current length of the queue.
   * 
   * @return The length of the queue.
   */
  public int length() {
    return numElems;
  }

  /**
   * This method provides a string representation of the queue.
   * 
   * @return The String representation of the queue.
   */
  public String toString() {

  }

}

这是它实现的接口:

package csc143.data_structures;

public interface BoundedQueue<T> {

  /**
   * This method inserts the specified element, unless the
   * queue is full.
   * 
   * @param o The element to be inserted.
   * @throws FullQueueException If the queue is full.
   */
  public void insert(T o) throws FullQueueException;

  /**
   * This method returns the element at the front of the
   * queue, unless the queue is empty.
   *
   * @return The element at the front of the queue. 
   * @throws EmptyQueueException If the queue is empty.
   */
  public T front() throws EmptyQueueException;

  /**
   * This method retrieves and removes the element at the front
   * of the queue, unless the queue is empty.
   * 
   * @return The element at the front of the queue.
   * @throws EmptyQueueException If the queue is empty.
   */
  public T remove() throws EmptyQueueException;

  /**
   * This method reports whether or not the queue contains
   * element(s).
   * 
   * @return If one or more element exists or not.
   */
  public boolean hasMember();

  /**
   * This method reports whether the queue has space to add
   * element(s).
   * 
   * @return If space exists or not.
   */
  public boolean hasSpace();

  /**
   * This method returns the capacity of the queue.
   * 
   * @return The capacity of the queue.
   */
  public int capacity();

  /**
   * This method returns the current length of the queue.
   * 
   * @return The length of the queue.
   */
  public int length();

  /**
   * This method provides a string representation of the queue.
   * 
   * @return The String representation of the queue.
   */
  public String toString();

}
4

3 回答 3

7
  1. 您不能创建泛型类型参数的数组。
  2. 您不能使用类型参数作为其参数声明方法,该参数将在擦除后与现有方法的签名匹配。发生这种情况是因为您正在实现原始类型。

所以,你应该像这样声明你的班级 -

public class BoundedQueueArray<T> implements BoundedQueue<T>

并从构造函数中删除以下数组创建代码 -

elements = new T[capacity];

如果您可以使用List( interface , implementation ) 代替数组(参见Effective Java,Item 25),并尽可能远离原始类型(参见Effective Java,Item 23),那就更好了。

例子 -

private List<T> elements;    // convert array to list

和 -

elements = new ArrayList<T>();    // create instance like this
于 2013-08-01T08:06:40.803 回答
0
private T[] elements;

这条线是问题所在。据我所知,不允许使用泛型数组。

改用Lista

于 2013-08-01T08:08:35.887 回答
0

正如我在评论中所说,去看看 ArrayList 的实现有一些想法

正如java中的每个对象都实现的那样Object,在ArrayList中你可以看到元素存储在Object[]

private transient Object[] elementData;

正如你知道数组的类型,你可以毫无问题地初始化它

您的插入将完全相同,无论 T 是什么,它将始终继承 Object

从 ArrayList 中取出对象也很简单,您需要做的就是将其转换为您的泛型类型

     public E get(int index) {  
       RangeCheck(index); //irrevelant for your example
       return (E) elementData[index];
     }

如果我错过了什么,去看看这里

于 2013-08-02T08:40:59.010 回答