Solved

Java Iterating over Collections and Arrays, and maybe other things

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

734 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