• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[duplicate of Matthew's post]
 
reply
    Bookmark Topic Watch Topic
  • New Topic