• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning String[] to multiple variables

 
Rob Marshall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. You can't do it in Java.
 
Rob Marshall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that was quick.

Thanks for the fast response.

Rob
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can ofcourse do:

But as Freddy said, Java has no syntax to do it in one step.
 
Rob Marshall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic