| Author |
treeset question
|
geeta vemula
Ranch Hand
Joined: Jul 18, 2008
Posts: 208
|
|
I got this from K and B master exam (learnkey) private Set<Integer> numbers=new TreeSet<Integer>(); public Nmbers(int... nums) { for(int n: nums) numbers.add(n); } public Numbers negate() { Numbers negatives=new Numbers(); //Line 1 for(int n: numbers) negatives.numbers.add(-n); return negatives; } public void show() { for(int n : numbers) System.out.println(n+""); } public static void main(String[] args) { new Numbers(1,3,-5).negate().show(); } } Answer is : -3 -1 5 In this at line 1 it is trying to call Numbers negatives=new Numbers(); (no arg constructor) then won't it give compiler error?
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
I am also surprised to this question,because compiler wont create no-arg constructor if we created one.In this case how it is possible (numbers n=new numbers() invoking show()? Thanks preetha
|
 |
Himanshu Gupta
Ranch Hand
Joined: Aug 18, 2008
Posts: 598
|
|
Where is the Numbers Class? If they are same then it is legal to not pass any arguments if your constructor is taking var-args. You may or may not pass any number of arguments. [ December 20, 2008: Message edited by: Himanshu Gupta ]
|
My Blog SCJP 5 SCWCD 5
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
Ya , i got it. it can take zero as argument.so it i possible to create no-arg constructor.am i right? Thanks Preetha
|
 |
Himanshu Gupta
Ranch Hand
Joined: Aug 18, 2008
Posts: 598
|
|
YES you are right.
|
 |
geeta vemula
Ranch Hand
Joined: Jul 18, 2008
Posts: 208
|
|
yes i got it.. Thanks, Geeta Vemula
|
 |
 |
|
|
subject: treeset question
|
|
|