簡體   English   中英

類型不匹配:無法從元素類型對象轉換

[英]type mismatch: cannot convert from element type object

我正在開發一個簡單的游戲,但是在for-each循環中遇到類型不匹配錯誤(類型不匹配:無法從元素類型對象轉換為IWeapon):

IList<IWeapon> LoW = t.incomingQueue.get(turn);

    if (LoW.isCons()) {
        // sets item to be processed as the first item in the list
        for (IWeapon weapon : LoW) {
            // code here
        }

t.incomingQueue是ArrayList>,因此它將始終是IList。 無論如何我都嘗試投射,但是仍然出現錯誤。

我認為這可能與我創建列表迭代器的方式有關,因此我將其包含在此處:

interface IList<T> extends Iterable {

// checks if a given item is in the list
boolean isIn(T item);

// checks if the list is a cons or not
boolean isCons();

Cons<T> asCons();

// an iterator 
Iterator<T> iterator();

// checks if list has another value left
boolean hasNext();

// gets data value at this point
T getData();

// gets the rest of the list
IList<T> getNext();
}

// an empty list
class Empty<T> implements IList<T> {

// an item cannot be in an empty list
public boolean isIn(T item) {
    return false;
}

// an empty list is not a cons
public boolean isCons() {
    return false;
}

public Cons<T> asCons() {
    throw new UnsupportedOperationException("Can't call empty as a cons");
}

// for iterating over the list
public Iterator<T> iterator() {
    return new ListIterator<T>(this);
}

// an empty list does not have a next item
public boolean hasNext() {
    return false;
}

// won't be used since an iterator will never access an empty list
public T getData() {
    return null;
}

// will never be reached
public IList<T> getNext() {
    throw new UnsupportedOperationException("Can't get next of an empty        list.");
  }
 }

// a list with item(s)
class Cons<T> implements IList<T> {
// the first item in this list
T first;
// the rest of a list is either a list with items or an empty list
IList<T> rest;

// constructor statement
Cons(T first, IList<T> rest) {
    this.first = first;
    this.rest = rest;
}

// an item is in a list if it is the first item, or if it's in the rest of the list
public boolean isIn (T item) {
    return this.first.equals(item) || this.rest.isIn(item);
}

// a cons list is a cons list
public boolean isCons() {
    return true;
}

public Cons<T> asCons() {
    return this;
}

// for iterating over the list
public Iterator<T> iterator() {
    return new ListIterator<T>(this);
}

// a list with items has a next value
public boolean hasNext() {
    return true;
}

// gets data value at this point
public T getData() {
    return this.first;
}

// gets the rest of the list
public IList<T> getNext() {
    return this.rest;
}
}

//for iterating over our list
class ListIterator<T> implements Iterator<T> {
 // the current list
 IList<T> curr;

 // constructor
 ListIterator(IList<T> curr) {
 this.curr = curr;
 }

// returns true if there's at least one value left in this iterator
 public boolean hasNext() {
 return curr.hasNext();
 }

 // returns the next value and advances the iterator
 public T next() {
 T temp = this.curr.getData();
 this.curr = this.curr.getNext();
 return temp;
 }

// no need to implement this since it is never used
public void remove() {
 throw new UnsupportedOperationException("Removing in IList iterator not    supported.");
}
}

問題是您必須擴展Iterable<T>而不是Iterable ,所以IList應該聲明為:

interface IList<T> extends Iterable<T> {

代替

interface IList<T> extends Iterable {

本身可迭代(不帶參數)是指原始類型, 在某種程度上 ,它是一種由對象類型參數化的可迭代參數。

您的設計非常好奇。 是否有理由建立自己的列表而不使用另一個ArrayList?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM