| Author |
java pass by value.
|
jing hu
Ranch Hand
Joined: Jun 30, 2009
Posts: 63
|
|
I want to assign set. But now it doesn't meet my requirement.
when using set = newSet; it doesn't change.
I know the reason. I want to know if there is a convenient method assign set with newSet.
Thanks.
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14669
|
|
If the Set you pass to the method is not null, you could call set.clear().
But you could also return a Set, instead of passing it as a parameter.
|
[My Blog]
All roads lead to JavaRanch
|
 |
jing hu
Ranch Hand
Joined: Jun 30, 2009
Posts: 63
|
|
yeah,I can return the newSet directly in this case.
But this is just a example. in the reality, Maybe I can't modify the function return type or there is already an returned type.
So any other method?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14669
|
|
Not that I can think of. "Clear and Add" or "Create, Add and Return". That looks enough to me.
Maybe I can't modify the function return type or there is already an returned type.
Then you'd probably pass an existing Set, or encapsulate the Set in a object and pass the object to the method.
|
 |
jing hu
Ranch Hand
Joined: Jun 30, 2009
Posts: 63
|
|
ok.
I can write like this.
set.clear();
set.addAll(newSet);
what about the code efficiency ?
|
 |
 |
|
|
subject: java pass by value.
|
|
|