【问题标题】:type mismatch: cannot convert from element type object类型不匹配:无法从元素类型对象转换
【发布时间】:2016-07-19 17:48:27
【问题描述】:

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

【问题讨论】:

  • t.incomingQueue 是什么?是如何添加到其中的?
  • 你刚刚错过了我的编辑! t.incomingqueue 是一个 ArrayList>。我还没有编写向其中添加内容的函数

标签: java


【解决方案1】:

问题是你必须扩展Iterable&lt;T&gt;而不是Iterable,所以IList应该声明为:

interface IList<T> extends Iterable<T> {

而不是

interface IList<T> extends Iterable {

Iterable 本身(不带参数)指的是原始类型,在某种程度上是一个由 Object 类型参数化的可迭代。

你的设计很奇怪。您构建自己的列表而不是使用另一个 ArrayList 是否有原因?

【讨论】:

  • 这行得通,谢谢!对于你关于我的设计的问题,我真的不确定。还是一个新开发者,所以我想我只是想尝试一下
猜你喜欢
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 2014-05-19
相关资源
最近更新 更多