Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using for..each loop on Object[]

 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that by using Integer[] instead of Object[] will work in for...each loop. But what about Object[] ?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, in the last post, " " was misinterpreted as
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin,
Here is the code I wanted to post earlier:


The compile error is on line marked 1.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidhya,

You'll still need to use the cast that Jesper mentioned earlier.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic