for each for arraylist but not for arrays

Hi,

I wonder why 'for each' loop used for only arraylist but not for arrays. For what other collection types we can use 'for each' . please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
public static void main(String[] args){
      int [] array={2,7,1,8,2,8};
      for( int i : array ){
          System.out.print( i );
        }
}
works fine
gudii9Author Commented:
Ok. Can we use it with all collection types?
ozoCommented:
What are you trying to do that you cannot do?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Specifically, the foreach loop construct can be used for any array and any object that implements the "Iterable" interface. The List interface inherits from Iterable so therefore any List implementation can be used, and the same for Set implementations. However such things as Map's don't so you can't directly use them in foreach loops, but they do provide methods such as, entrySet(), keySet() and values(), that do return Iterable views of the contents and so they CAN be used in foreach loops.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dung TrinhMarching Band Field CrewCommented:
For each can be use on any data structure that implements Iterable. You can read this link to have more understand about classes that use foreach
Array is an exception when it doesn't implements Iterable but still can use for each loop
gudii9Author Commented:
For each can be use on any data structure that implements Iterable. You can read this link to have more understand about classes that use foreach
Link is missing. Please provide
gudii9Author Commented:
any object that implements the "Iterable" interface.
where to find list of all objects that implements 'iterable' interface. please advise
gurpsbassiCommented:
Have you tried reading the Java API docs?
awking00Information Technology SpecialistCommented:
From Java documentation -

Interface Iterable<T>

All Known Implementing Classes:
AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayBlockingQueue, ArrayDeque, ArrayList, AttributeList, BatchUpdateException, BeanContextServicesSupport, BeanContextSupport, ConcurrentLinkedQueue, ConcurrentSkipListSet, CopyOnWriteArrayList, CopyOnWriteArraySet, DataTruncation, DelayQueue, EnumSet, HashSet, JobStateReasons, LinkedBlockingDeque, LinkedBlockingQueue, LinkedHashSet, LinkedList, PriorityBlockingQueue, PriorityQueue, RoleList, RoleUnresolvedList, RowSetWarning, SerialException, ServiceLoader, SQLClientInfoException, SQLDataException, SQLException, SQLFeatureNotSupportedException, SQLIntegrityConstraintViolationException, SQLInvalidAuthorizationSpecException, SQLNonTransientConnectionException, SQLNonTransientException, SQLRecoverableException, SQLSyntaxErrorException, SQLTimeoutException, SQLTransactionRollbackException, SQLTransientConnectionException, SQLTransientException, SQLWarning, Stack, SyncFactoryException, SynchronousQueue, SyncProviderException, TreeSet, Vector
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.