• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Error] Can only iterate over an array or an instance of java.lang.Iterable

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to transverse an ArrayList with a for..each loop, but Eclipse complained:


Error:
Can only iterate over an array or an instance of java.lang.Iterable


NOTES
1. According to the Java API for 1.6, ArrayList implements Iterable<E> - http://download.oracle.com/javase/6/docs/api.
2. Java Notes, For-each loop at http://leepoint.net/notes-java/flow/loops/foreach.html.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are attempting to iterate over players.get(i); I don't know what that returns, but your error message indicates it is not iterable. I would have expected it to be a player, and a Player object to have a method that returns a collection of cards (getCards(), getHand(), something like that). Perhaps you need something like players.get(i).getCards().

rc
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to traverse the players list, you just need:
If you want to iterate over the cards owned by each player, as Ralph says, it depends on what methods you're exposing in Player. Maybe:
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[duplicate of Matthew's post]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic