Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

how to write a method to return 2 values

Thread view: 
Thanasis \(sch\) - 24 Feb 2005 21:56 GMT
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 - 24 Feb 2005 22:02 GMT
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.
Thanasis \(sch\) - 25 Feb 2005 18:10 GMT
thanks
> 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 - 25 Feb 2005 00:07 GMT
"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.
Eric Sosman - 25 Feb 2005 16:50 GMT
> 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.

Signature

Eric.Sosman@sun.com

Thanasis \(sch\) - 25 Feb 2005 18:10 GMT
>> 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 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
Eric Sosman - 25 Feb 2005 18:38 GMT
Thanasis (sch) wrote:
>  >> is it possible to write a method in Java such that it returns more than
> one
[quoted text clipped - 9 lines]
> 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.

Signature

Eric.Sosman@sun.com

Wiseguy - 25 Feb 2005 18:54 GMT
Eric Sosman <eric.sosman@sun.com> scribbled on the stall wall:

>     How about
>
[quoted text clipped - 9 lines]
> ?  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.
Paul van Rossem - 25 Feb 2005 19:48 GMT
> Eric Sosman <eric.sosman@sun.com> scribbled on the stall wall:
>
[quoted text clipped - 19 lines]
> 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.
Thanasis \(sch\) - 26 Feb 2005 00:03 GMT
 >    How about

> /** Find the leftmost unvisited node adjacent to a
> *    given node.
[quoted text clipped - 7 lines]
> ?  Unless the graph can legitimately contain `null' nodes,
> this seems a natural approach.

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
Patricia Shanahan - 26 Feb 2005 00:25 GMT
Thanasis (sch) wrote:

>   >    How about
>
[quoted text clipped - 20 lines]
> 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
Bryce - 25 Feb 2005 19:52 GMT
> >> is it possible to write a method in Java such that it returns more than
>one
[quoted text clipped - 8 lines]
>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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.