Solved

Java Iterating over Collections and Arrays, and maybe other things

Posted on 2014-09-30
2
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

732 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