Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java pass by value.

 
jing hu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jing hu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.
I can write like this.

set.clear();
set.addAll(newSet);

what about the code efficiency ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic