1

I have the error:

 Type mismatch: cannot convert from element type Object to Card 

with the following code:

Deck Removal
....
for (Card c : Removal)
        d.removeCard(c);

This is the iterator method in my deck class:

@Override
public Iterator<Card> iterator()
{
    return deck.iterator();
}

The underlying structure for the deck is Vector<Card>. My iterator is returning type Card. Why is it telling me it's Object in the for loop?

I can do the following and then the error would go away but that's quite stupid since I'm already supposed to return Cards instead of Objects.

Deck Removal
....
for (Object c : Removal)
        d.removeCard((Card)c);

Thanks

3
  • 3
    Does Deck implement Iterable or Iterable<Card>? Commented Oct 6, 2014 at 10:02
  • Great! In that case, I'll write my comment as an answer. Commented Oct 6, 2014 at 10:05
  • fyi Vector is only used in out-of-date educational materials. In the real world, it is a long-abandoned, deprecated and broken class. Commented Oct 6, 2014 at 12:08

1 Answer 1

3

In order for the enhanced for loop over Deck to return Card objects, Deck should implement Iterable<Card>, instead of just Iterable.

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

Comments

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.