0

I'm working on a simple game but I'm getting a type-mismatch error in my for-each loop (type mismatch: cannot convert from element type object to 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 is a ArrayList> so it will always be an IList. I tried casting anyways but I still got the error.

I think it might have to do with how I created the list iterator so I've included that here:

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.");
}
}
2
  • What is t.incomingQueue and how are things added to it? Commented Jul 19, 2016 at 17:51
  • you just missed my edit! t.incomingqueue is a ArrayList<IList<IWeapon>>. I haven't yet coded the function which adds things to it Commented Jul 19, 2016 at 17:54

1 Answer 1

2

The problem is you must extend Iterable<T> and not Iterable, so IList should be declared as:

interface IList<T> extends Iterable<T> {

instead of

interface IList<T> extends Iterable {

Iterable by itself (without the parameter) refers to the raw type, which is in in a way an iterable parametrized by type Object.

Your design is rather curious. Is there a reason why you built your own list instead of using another ArrayList?

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thanks! For your question about my design, I really am not sure. Still a new developer so I think I just wanted to try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.