Hi there!!
Just wanted to ask please if this is applicable to return multiple values from a method in java as it is in php's functions for instance.
Thanks in advance!!
Atar.
Printable View
Hi there!!
Just wanted to ask please if this is applicable to return multiple values from a method in java as it is in php's functions for instance.
Thanks in advance!!
Atar.
How would you do this in php? As far as I know, and unless the php docs are out of date, you cannot
PHP: Returning values - Manual
In java you could effectively do this just as you would in php - by wrapping the values in something else, for instance an array, a Collection, or wrap the values within a particular object.Quote:
A function can not return multiple values
Please elaborate...
What are you trying to do? I'm sure there is a way, I just don't know what you're asking.
@copeg
Thank you about your quick response!!
Can you post here a little example about how to do so with arrays, collections that you spoke about?
Your help is appreciated!!
Atar.
What copeg said is something like this.Imagine that you want to return 5 values,e.g. 0,1,2,3,4 then you would write something like this
Code :static void f(int[] a) { for(int i=0 ; i<a.length ; i++) a[i]=i; } public static void main(String[] args) { int []a = new int[5]; f(a); }
If you do not understand something ask :D
@Samaras
All right, but how should I return those multiple values? There's not any reference to that in your code.
Now array a holds these values.So you have access to them via array a.Try writing in main after f(a)
This should output 0,1,2,3,4.So you have access in these values that were created inside function f.Isn't that what you wanted,or i understood wrongly? :/Code :for (int i = 0; i < a.length; i++) { System.out.println(a[i]);
@Samaras: that's not necessarily returning a value, you're simply modifying one. I know it's common practice in C, but there are a few caveats with this method.
The first is you can't actually change what array you have, so the array you're passed is the array you have to modify.
For example:
Code java:
When you run this code, you'll notice that the program prints out five 0's, not five 3's.
The better method is to instantiate and return said array:
Code java:
Note that this code still only returns one value: it's returning an array. However, an array is composed of multiple elements, which can be extracted.
Now if you have to return a bunch of values who's type do not correlate, the best practice is to create a class which holds all of the necessary components. For example, take the following class:
Using a similar method as above, you can create and instantiate a Customer object and return it. Again, technically you're only returning one value: a customer object. However, inside that object there is a name, id, and pay fields.
Ok if you return the array or pass it as an argument does not make much difference at this point i would say.
However the example you gave me shocked me a bit.If i comment this line var = new int[5]; ,then the output will be 3's and no 0's.If i uncomment it the output is going to be as you said.WHY?
What if the called method wants to set the size of the array it returns?Quote:
if you return the array or pass it as an argument does not make much difference
.I run it and i will say that if passed as an argument the size won't change.If it is been returned the size is going to change.WHY?
In java i have been told that the arguments are passed as a reference..Is that true?And somehow is this link to this somehow?
Remember i am a BEGINNER˛ :D
There might not be an array passed to the method. It all depends on what the method is supposed to do.Quote:
If it is been returned the size is going to change.
I would change that to say the size of the returned array would be set by the called method.
Args are passed by value.
ok i agree,but my question was not answered yet i think :/
Start your own thread if you have questions. This one was started by atar.