Java 从接口实现通用方法?

Java 从接口实现通用方法?,java,Java,我正试图实现一个自创的通用接口“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

我正试图实现一个自创的通用接口“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() {

  }

}
包csc143.data\u结构;
公共类BoundedQueueArray实现BoundedQueue{
//数组中存储的元素
私有T[]元素;
//队列中当前的元素数
私人国际婚礼;
public BoundedQueueArray(整数容量){
//实例化并绑定到引用
元素=新的T[容量];
numlems=0;
}
/**
*此方法插入指定的元素,除非
*队列已满。
* 
*@param o要插入的元素。
*@如果队列已满,则引发FullQueueException。
*/
公共无效插入(TO)引发FullQueueException{
if(numlems0;
}
/**
*此方法报告队列是否有要添加的空间
*元素。
* 
*@返回是否存在空格。
*/
公共布尔hasSpace(){
return elements.length-length()>0;
}
/**
*此方法返回队列的容量。
* 
*@返回队列的容量。
*/
公共交通容量(){
返回元素长度;
}
/**
*此方法返回队列的当前长度。
* 
*@返回队列的长度。
*/
公共整数长度(){
回礼;
}
/**
*此方法提供队列的字符串表示形式。
* 
*@返回队列的字符串表示形式。
*/
公共字符串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();

}
包csc143.data\u结构;
公共接口边界队列{
/**
*此方法插入指定的元素,除非
*队列已满。
* 
*@param o要插入的元素。
*@如果队列已满,则引发FullQueueException。
*/
公共void insert(to)抛出FullQueueException;
/**
*此方法返回位于
*队列,除非队列为空。
*
*@返回队列前面的元素。
*@如果队列为空,则抛出EmptyQueueException。
*/
public T front()抛出EmptyQueueException;
/**
*此方法检索并删除前面的元素
*除非队列为空,否则为队列的。
* 
*@返回队列前面的元素。
*@如果队列为空,则抛出EmptyQueueException。
*/
public T remove()抛出EmptyQueueException;
/**
*此方法报告队列是否包含
*元素。
* 
*@返回是否存在一个或多个元素。
*/
公共布尔hasMember();
/**
*此方法报告队列是否有要添加的空间
*元素。
* 
*@返回是否存在空格。
*/
公共布尔hasSpace();
/**
*此方法返回队列的容量。
* 
*@返回队列的容量。
*/
公共交通容量();
/**
*此方法返回队列的当前长度。
* 
*@返回队列的长度。
*/
公共整数长度();
/**
*此方法提供队列的字符串表示形式。
* 
*@返回队列的字符串表示形式。
*/
公共字符串toString();
}
  • 不能创建泛型类型参数的数组
  • 不能声明类型参数为参数的方法,该参数将在擦除后与现有方法的签名匹配。这是因为您正在实现一个原始类型
  • 所以,你应该这样声明你的类-

    public class BoundedQueueArray<T> implements BoundedQueue<T>
    
    如果您可以使用
    列表
    (,)代替数组(请参见第25项),并尽可能远离原始类型(请参见第23项),则效果会更好

    示例-

    private List<T> elements;    // convert array to list
    
    私有列表元素;//将数组转换为列表
    
    及-

    elements = new ArrayList<T>();    // create instance like this
    
    elements=new ArrayList();//创建这样的实例
    
    这条线就是问题所在。据我所知,通用数组是不允许的


    使用
    列表
    代替

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

    由于java中的每个对象都实现了
    object
    ,所以在ArrayList中可以看到元素存储在
    object[]

    private transient Object[]elementData

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

    您的插入将完全相同,无论T是什么,它都将继承对象

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

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

    如果我遗漏了什么,就去看一看

    谢谢,这很有效。如何在构造函数中实例化数组?感谢您的帮助。另外,是否可以用不同的方法完成此任务,并且仍然使用数组作为底层结构?@user2581779:
    ArrayListprivate T[] elements;
    
         public E get(int index) {  
           RangeCheck(index); //irrevelant for your example
           return (E) elementData[index];
         }