类型不匹配:无法从元素类型对象转换

时间:2016-07-19 17:48:27

标签: java

我正在开发一个简单的游戏,但是我的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
        }

.incomingQueue是一个ArrayList&gt;所以它永远是一个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.");
}
}

1 个答案:

答案 0 :(得分:2)

问题是您必须扩展Iterable<T>而不是Iterable,因此IList应声明为:

interface IList<T> extends Iterable<T> {

而不是

interface IList<T> extends Iterable {

自身可迭代(没有参数)是指原始类型,它在中的某种方式是由Object类型参数化的可迭代类型。

你的设计很好奇。你有没有理由建立自己的列表而不是使用另一个ArrayList?