aspose file tools*
The moose likes Beginning Java and the fly likes java pass by value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java pass by value." Watch "java pass by value." New topic
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: 14687
    
  16

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: 14687
    
  16

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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java pass by value.
 
Similar Threads
Question About TreeSet?
HashSet and Strings doubt
HashSet question
Why doesn't Object type come out of a non-generic list?
Get the Element from Set