単純なゲームで作業していますが、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は、常に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'とは何ですか?物事はどのように追加されますか? – bradimus
あなたは私の編集を見逃しました! t.incomingqueueはArrayList>です。私はまだそれに事を追加する関数をコード化していません –
kathanm