Solved

Java Iterating over Collections and Arrays, and maybe other things

Posted on 2014-09-30
2
163 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 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 40353529
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
ID: 40353889
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

828 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