aspose file tools*
The moose likes Beginning Java and the fly likes [Error] Can only iterate over an array or an instance of java.lang.Iterable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Error] Can only iterate over an array or an instance of java.lang.Iterable " Watch "[Error] Can only iterate over an array or an instance of java.lang.Iterable " New topic
Author

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

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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.

Jon
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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

Joined: Apr 06, 2010
Posts: 4465
    
    8

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
[duplicate of Matthew's post]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Error] Can only iterate over an array or an instance of java.lang.Iterable