File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Assigning String[] to multiple variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Assigning String[] to multiple variables" Watch "Assigning String[] to multiple variables" New topic
Author

Assigning String[] to multiple variables

Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Hi,

I've just started playing with Java so this question may have been asked elsewhere...I just couldn't find it. But I would be very grateful for a pointer

In Perl and Python it's possible to have a subroutine/method return a list and assign the return to multiple variables, e.g.:

(var1, var2, var3) = getListWithThreeStrings();

Is there something similar in Java?

Thanks,

Rob
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Nope. You can't do it in Java.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Wow, that was quick.

Thanks for the fast response.

Rob
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to JavaRanch

You can return an object which encapsulates those three values, or an array. [A 3-member array is an object which encapsulates three values.]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

You can ofcourse do:

But as Freddy said, Java has no syntax to do it in one step.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Hi Campbell,

Duh! You're absolutely correct...I should have thought of that. I guess I'm still not thinking "object oriented"

And Jesper, you're correct as well. I just find that ugly Not that I don't already write lots of "ugly" code in so many ways

Thanks,

Rob
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning String[] to multiple variables