gpt4 book ai didi

java - 类型不匹配 : cannot convert from element type object

转载 作者:行者123 更新时间:2023-12-02 03:28:24 25 4
gpt4 key购买 nike

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

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

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

关于java - 类型不匹配 : cannot convert from element type object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com