• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem when using ternary operatory in For Each loop  RSS feed

 
Anish Kurian Thomas
Greenhorn
Posts: 21
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am having an issue with the for each loop here. The code which is shown below is throwing a compilation time error message that "Can only iterate over an array or an instance of java.lang.Iterable"




But I am not getting any error if I change the code like below:



or



Could any one help me in understanding the cause of this error?

Thank you in advance,
Anish
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think the compiler thinks the type is of:

anObject.getClass().isArray() ? (Object[])anObject : (List)anObject

?

It can't be Object[], because it might return a List. It can't be a List, because it might return an Object[]. The only way the compiler can reconcile these is to assume the type is Object, as that's a common superclass.

And, obviously, you can't iterate over an Object.

Whereas when both alternatives have the same type, that's the type the compiler will use for the whole expression. So it can compile in both those cases.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really needed to get this to work, you could probably try this (I haven't actually confirmed it though):
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better would be to make the array into a list, or vice versa, before you use the foreach.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should work. When in doubt (and just to make it easier to read, I'd definitely do this), extract the declaration to outside the loop:
Also note that my example has removed the unchecked conversion warning, but that can be added to Matthew's code as well:
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find all the above hard to read and comprehend. Why not something like:

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1 -- that original code is essentially impossible to read.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:I find all the above hard to read and comprehend.

True, good point. That's what I'd do (with a few added { }s ) in a "real" situation.
 
Anish Kurian Thomas
Greenhorn
Posts: 21
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you every body especially Matthew. Now I understand the reason. Thanks once again
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note: generally in ternary operator [condition ? X : Y ], It is good Idea to try to use X and Y as a same type.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!