For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > how to write a method to return 2 values









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to write a method to return 2 values
Thanasis \(sch\)

2005-02-24, 8:59 pm


Hi to all,

is it possible to write a method in Java such that it returns more than one
values of different type each (i.e a boolean and an integer)?How this could
be done?


thanks in advance
thanasis


klynn47@comcast.net

2005-02-24, 8:59 pm

No, you can't have more than one thing returned from the method. You
could encapsulate the two variables into an object and return the
object.

Wiseguy

2005-02-24, 8:59 pm

"Thanasis \(sch\)" <thanasis_gr@hotmail.com> scribbled on the stall wall:
>
> Hi to all,
>
> is it possible to write a method in Java such that it returns more than one
> values of different type each (i.e a boolean and an integer)?How this could
> be done?
>


As klynn74 mentioned: the way to do that is to make your method return
an object of a class that contains the two variables.

Another (ungracefull) way is to return an array of objects and type cast
the returned objects to whatever native types they are suppose to be.

Note that asking a method to return multiple values signifies to me a
poor design. Think about why you feel that you need to do this and
reevaluate.



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Eric Sosman

2005-02-25, 4:02 pm



Wiseguy wrote:
>
> Note that asking a method to return multiple values signifies to me a
> poor design. Think about why you feel that you need to do this and
> reevaluate.


See java.math.BigInteger#divideAndRemainder() for
an example of a single computation that produces two
outputs.

--
Eric.Sosman@sun.com

Thanasis \(sch\)

2005-02-25, 4:02 pm

>> is it possible to write a method in Java such that it returns more than
one[color=darkred]


thanks for reply. i am writing visualization software for graph algorithms
and i need a method to accomplish 2 tasks:

1) given a graph's node i want my method to determine (yes/no) whether
exists an unvisited adjacent node
2) return the leftmost unvisited adjacent node


Having pascal programming background i think that the java design team must
have predicted that situation :)

wishes
thanasis


Thanasis \(sch\)

2005-02-25, 4:02 pm

thanks
<klynn47@comcast.net> wrote in message
news:1109282547.416944.295940@g14g2000cwa.googlegroups.com...
> No, you can't have more than one thing returned from the method. You
> could encapsulate the two variables into an object and return the
> object.
>



Eric Sosman

2005-02-25, 4:02 pm



Thanasis (sch) wrote:
> one
>
>
>
>
> thanks for reply. i am writing visualization software for graph algorithms
> and i need a method to accomplish 2 tasks:
>
> 1) given a graph's node i want my method to determine (yes/no) whether
> exists an unvisited adjacent node
> 2) return the leftmost unvisited adjacent node


How about

/** Find the leftmost unvisited node adjacent to a
* given node.
* @param node The given node.
* @returns The leftmost unvisited node adjacent
* to <code>node</code>, or <code>null</code>
* if no such node exists.
*/
Node leftmostUnvisitedAdjacent(Node node) { ... }

? Unless the graph can legitimately contain `null' nodes,
this seems a natural approach.

--
Eric.Sosman@sun.com

Wiseguy

2005-02-25, 4:02 pm

Eric Sosman <eric.sosman@sun.com> scribbled on the stall wall:
>
> How about
>
> /** Find the leftmost unvisited node adjacent to a
> * given node.
> * @param node The given node.
> * @returns The leftmost unvisited node adjacent
> * to <code>node</code>, or <code>null</code>
> * if no such node exists.
> */
> Node leftmostUnvisitedAdjacent(Node node) { ... }
>
> ? Unless the graph can legitimately contain `null' nodes,
> this seems a natural approach.


I agree. With the purpose he outlines he doesn't really need two return
values. Either your way, or having the method throw an exception for
(no nodes found) would be more intuitive than a boolean status returned.



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Paul van Rossem

2005-02-25, 9:01 pm

On 25-02-2005 19:56, Wiseguy wrote:
> Eric Sosman <eric.sosman@sun.com> scribbled on the stall wall:
>
>
>
> I agree. With the purpose he outlines he doesn't really need two return
> values. Either your way, or having the method throw an exception for
> (no nodes found) would be more intuitive than a boolean status returned.
>
>
>
> ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Of course. If you think about it, what would the node parameter return
if the boolean returned false??? So there is clearly no reason to return
two parameters.
Paul.
Bryce

2005-02-25, 9:01 pm

On Fri, 25 Feb 2005 20:12:24 +0200, "Thanasis \(sch\)"
<thanasis_gr@hotmail.com> wrote:

>one
>
>
>thanks for reply. i am writing visualization software for graph algorithms
>and i need a method to accomplish 2 tasks:
>
>1) given a graph's node i want my method to determine (yes/no) whether
>exists an unvisited adjacent node
>2) return the leftmost unvisited adjacent node


Return null if it doesn't exist and the leftmost node if it does?

--
now with more cowbell
Patricia Shanahan

2005-02-25, 9:01 pm

Thanasis (sch) wrote:

>
>
>
>
>
> hi eric,
>
> thanks again for reply.
> i had already found solution using 2 methods.
> One that finds out if an adjacent unvisited node exists and a second one
> that actually returns the leftmost adjacent unvisited node.
> I think your solution does work.
>
> regards
> thanasis
>
>



You do need to decide what the leftmost adjacent unvisited
node method does when there is no such node. If there is a
separate check method, the balance may shift in favor of
treating it as an exception.

Incidentally, there is another pattern that might be
appropriate, depending on the wider context. Have you
considered giving your structure the ability to return an
iterator that visits the nodes in leftmost order? The
java.util source code contains several implementation examples.

Patricia

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com