• Post Reply Bookmark Topic Watch Topic
  • New Topic

toArray method create problem in casting as prior to java5  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above program, it is not printing any output,because "if" body is not working. but when we remove the line 10 /*oa =iL.toArray();*/ then "if" body is working and output is
null
null
null
i cant understand why this happening?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 9, you make oa refer to a new array of Integer objects.

In line 10, you make oa refer to the result of whatever iL.toArray() returns. So, what you did in line 9 is overwritten by what you did in line 10. So, there's no reason anymore to exepct that the if-statement in line 11 will evaluate to 'true' if you do that.

In other words: You create a box named 'oa'. First you put a red ball in the box. Then you replace the red ball with a blue ball. And then you're surpised that there's no red ball in the box anymore?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to have a look at the overloaded toArray() method in the List class. That can be used to make sure your array is castable to a particular type of array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!