Solved

Java Iterating over Collections and Arrays, and maybe other things

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

910 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

20 Experts available now in Live!

Get 1:1 Help Now