Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not sure what is right?

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

import java.util.*;

public class Collect {
public static void main(String[] args)
{
Set<String> s=new HashSet<String>();
s.add("apple");
s.add("Apple");
s.add("1");
s.add("1.0");

a//String []ss1=new String[4];
b//ss1=s.toArray(ss1);
c//for(String str:ss1)
//System.out.println(str);
String[]ss=(String[])s.toArray();//1
for(String str:ss)//2
System.out.println(str);//3

}
}
This question is from the Whizlabs 5.0 Simulator, where it is given that when 1,2,3 is tried then it compiles and gives the output "apple,Apple,1,1.0" while i tried it generated exception, but when i tried the a,b,c line it compiled correctly and output correcly as well,
My question is why 1,2,3 line when used it generates exception while both (abc and 123 )are right?
 
Jason Irwin
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags, it will make it much easier to read your code.

Well, what was the exception? That will give you big clue as to what went wrong. Clue: What is the return type of "toArray()"? What is the return type of "toArray(T[])"?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception you are getting will be classcastexception. Well this is because String[] and Object[] are not related types. you can check this by using

This will show that the Object[] cannot be casted to a String[].

correct form of //1,//2 and //3 is same to a,b and c.


The thing here is that all arrays of reference types are subtypes of Object[] not the reference type of any object say String array object.
 
Jason Irwin
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitish, that make it identical to a,b, c.

This would work too:Although I think we have now given far too much information in these answers.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i din't see that. Well i edited the post. btw Jason please see your PM.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Irwin wrote:Please use code tags, it will make it much easier to read your code.

Well, what was the exception? That will give you big clue as to what went wrong. Clue: What is the return type of "toArray()"? What is the return type of "toArray(T[])"?

To make it easier :
Object[] toArray()
T[] toArray(T[])
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic