This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using for..each loop on Object[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using for..each loop on Object[]" Watch "Using for..each loop on Object[]" New topic
Author

Using for..each loop on Object[]

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65


I want to use for..each loop on 'oa' object to print out the values. What do I need to do for that?
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
I know that by using Integer[] instead of Object[] will work in for...each loop. But what about Object[] ?
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Vidhya,

You can just use the enhanced for loop directly with the Object array, as shown here:

Feel free to post a follow-up question if this confuses you.


SCJP 5.0
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Thanks, Kelvin. But, I know that you can use an 'Object' object in for..each.
Sorry, I think I didn't put the question properly.
Is there a way to use a primitive int in the for..each loop with the Object[], by casting just as it is used here(the whole code) :
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Vidhya, from that code it is still not clear what you want to do. You create an Object[] oa, but you are not using oa anywhere in the code after creating it. Did you mean something else than what you wrote?

You cannot write for (int i : oa), because oa is an Object[] and there is no way to convert an Object to an int without casting.

You could write for (int i : (Integer[]) oa), but that is only going to work if oa really refers to an Integer[] (even though the type of the variable itself is Object[]). If it's not really an Integer[], you'll get a ClassCastException.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
ok, got it. I wanted to be able to use an int to navigate thru the Object[] using enhanced for loop. I think the only way to use an enhanced for loop with Object[] is for (Object o : oa). Right?
The below code when used alongwith the one given in my previous post compiles fine but #2 gives runtime exception.

I get the #1 line. But, I am not fully clear why #2 gives ClassCastException. Is it because Object[] is different from Integer[]?
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Vidhya Ramaswamy:
I get the #1 line. But, I am not fully clear why #2 gives ClassCastException. Is it because Object[] is different from Integer[]?

The problem is with your "Object[] oa = iL.toArray();" statement. When called with no parameters, the toArray() method will return an Object array, i.e. an array instantiated using something like "new Object[10]". You'll get a runtime exception when you cast this array to an Integer[], because it actually is an Object[]. This can be a little confusing, because each of the element objects in your Object[] is actually an Integer--but the type of the array object is Object[], and you're casting a reference to the array object itself, not to the element objects referenced by the array object.

To get around this problem, you'll need to use the overloaded version of toArray() that takes an array parameter and returns an array with the same type. Example: "Object[] oa = iL.toArray(new Integer[0]);"
[ December 12, 2007: Message edited by: Kelvin Lim ]
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Kelvin,
I tried assigning a new Integer array to the Object array as below.

But I am getting compiler error:
Type mismatch: cannot convert from element type Object to Integer

So, I guess only Object can be used for Object[] as in
for(Object o : oa)
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Sorry, in the last post, " " was misinterpreted as
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Kelvin,
Here is the code I wanted to post earlier:


The compile error is on line marked 1.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Vidhya,

You'll still need to use the cast that Jesper mentioned earlier.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Kelvin]: To get around this problem, you'll need to use the overloaded version of toArray() that takes an array parameter and returns an array with the same type. Example: "Object[] oa = iL.toArray(new Integer[0]);"

Wouldn't it be easier to declare oa with a more precise (and correct) type?

This way the declaration matches what it actually is, and there should be little need for casting after that.
[ December 12, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Jim Yingst:
[QB][b]Wouldn't it be easier to declare oa with a more precise (and correct) type?

Definitely. I intentionally left the array reference type as Object[] to minimize the change to Vidhya's code, so that he can see the immediate result of the change I was describing. But, yes, obviously we'll do it the way you described for real programs.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using for..each loop on Object[]
 
Similar Threads
Do arrays auto-expand if created using List.toArray()
toArray()
Why can't we use the IS-A test for the enhanced for-loop?
Error on K&B book (Java 6) page 579
Simple J2SE 5.0 Tiger Notes