Solved

Java Iterating over Collections and Arrays, and maybe other things

Posted on 2014-09-30
2
158 Views
Last Modified: 2014-09-30
I wanted to make a method that was very general, which could be passed a Collection, which I can iterate over, and the Collection may contain subcollections which can be iterated over. To make it as general as possible I wanted it to also handle a subcollection which is an array. Then I realized a simple array of integers doesn't fall under "Collection". (In fact I think a simple array of anything, such as an array of classes, doesn't fall under "Collection".)

I could just make a special check to see if I have an array instead of a Collection, but of course now I'm wondering how many other special cases are there that I'm forgetting? Is there an easier way? Or am I stuck with having code for iterating over a Collection, and other code for iterating over an array, and always wondering if there's some third type of entity I can iterate over but I'm forgetting. (Can it ever be proven that I've covered everything that can be iterated over?)
0
Comment
Question by:deleyd
2 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
Comment Utility
Check out the iterable interface:

http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html

This is the thing that lets you do the "for each" thing:

List<Integer> myList ;    // Implements 'iterable'

// So now we can do this...
for (Integer int : myList) {
}

It's the most general thing you can iterate over and includes more than just collections.

However, it does not include arrays.  That's because they are primitive types in Java - so there's no methods implemented on the array itself.  So you will need some special logic for iterating over an array.  One simple approach is to use Array.asList() to treat the array as a list and then iterate over that:
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)

Doug
0
 

Author Closing Comment

by:deleyd
Comment Utility
I like the Arrays.asList() idea. It almost works. Works nice for arrays of Objects. Doesn't work for primitive data types such as int (darn!). I assume it also doesn't work for {byte, short, long, float, double, char, boolean}.

I've looked for code to convert an array of int to a List, and there doesn't seem to be an easy way (unless some external library is added, such as Guava).

Darn! (I keep coming across these interesting cases which turn into academic questions.)

Guess I'll just have separate code handing iterating over a primitive array.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now