how to write a method to return 2 values

JAVA

    Sponsored Links

    Next

  • 1. Tomcat restart on web.xml change
    Hi, First of all, I am not a java programmer, only the sysadmin. We are using a java/tomcat application running on tomcat 4.1. Each time I edit one conf file (tomcat/webapps/myapp/WEB-INF/web.xml) tomcat restarts. Can someone tell me what I must change to prevent this behaviour ? I think something in server.xml or global web.xml but I can't figure what to change/add/insert/delete. Thanks. (sorry for dual post on cljp)
  • 2. PrintWriter as blob with mysql
    probably it's a poor design of my application, however ... in a class i create PrintWriter pw = new PrintWriter( new BufferedOutputStream(socket.getOutputStream(), 1024), true); then i "insert" it in a blob field in a mysql table the insertion seems ok, the field contains something like:"java.io.PrintWriter@c2ff5" in another class i read that blob field (and try to use it as a PrintWriter) Blob writerletto = rs.getBlob("blobfield"); but it contains:"com.mysql.jdbc.Blob@1adc30" !! and i'm not able to use it as a PrintWriter. I've tried something like pwTo=new PrintWriter(writerletto); but there is not such constructor. am idefinetely on the WRONG road ? thank you

how to write a method to return 2 values

Postby Thanasis (sch) » Sat, 26 Feb 2005 06:56:49 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 



Re: how to write a method to return 2 values

Postby klynn47 » Sat, 26 Feb 2005 07:02:27 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.


Re: how to write a method to return 2 values

Postby noone » Sat, 26 Feb 2005 09:09:13 GMT

"Thanasis \(sch\)" < XXXX@XXXXX.COM > scribbled on the stall wall:

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.**--****.com/  The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Re: how to write a method to return 2 values

Postby Eric Sosman » Sun, 27 Feb 2005 01:50:30 GMT





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

-- 
 XXXX@XXXXX.COM 


Re: how to write a method to return 2 values

Postby Thanasis (sch) » Sun, 27 Feb 2005 03:12:24 GMT

 >> is it possible to write a method in Java such that it returns more than 
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


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

wishes
thanasis 



Re: how to write a method to return 2 values

Postby Thanasis (sch) » Sun, 27 Feb 2005 03:13:10 GMT

thanks







Re: how to write a method to return 2 values

Postby Eric Sosman » Sun, 27 Feb 2005 03:39:01 GMT





    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.

-- 
 XXXX@XXXXX.COM 


Re: how to write a method to return 2 values

Postby noone » Sun, 27 Feb 2005 03:56:11 GMT

Eric Sosman < XXXX@XXXXX.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.**--****.com/  The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Re: how to write a method to return 2 values

Postby Paul van Rossem » Sun, 27 Feb 2005 04:48:29 GMT


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.

Re: how to write a method to return 2 values

Postby Patricia Shanahan » Sun, 27 Feb 2005 09:25:58 GMT





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


Similar Threads:

1.returning multiple values from a method

hi,

how would you return multiple values from a method?   i have written a 
method that needs to return three 3-dimensional arrays.  how would you do 
this other than forming another rather complex array holding the three to be 
returned?

i have programmed in php before and used 'lists' to do this sort of thing. 
does java have an analagous 'list' concept?

thanks 


2.returning multiple values from a method part ii

hi folks,

sorry for these annoying newbie questions, but hope you dont mind helping me 
out again.

i've written a method that returns three 3-dimensional arrays, and my 
original question was to how to return these arrays without putting them in 
another array and returning that.  someone mentioned the use of lists, which 
i have now successfully implemented, so my three arrays now reside in a list 
which is then returned by the method.

my problem now seems how to reference the arrays in the list once the list 
has been returned?   in the case of simple integer values say being in the 
list then this doesnt seem to be a problem.  if say the returned list was 
'result' , then the values can be displayed by 
system.out.println(result.get(0)), system.out.println(result.get(1))  etc..

but what if 'result' now itself contains arrays, then how are individual 
entries of these arrays accessed?

thanks 


3.implement IDL-struct as return value of method in java

Hi,

how could I implement IDL-struct as return value of method in java,
please:

///IDL

struct info{
 long ID
 string notice
}

interface XX{
Info delete(in DD, out KK);
}

thanks
Br,
Tina

4.Using a method to return a value, that uses a modal dialog

I started work today on a custom modal dialog class, and started to write:

SomeClass select () {
     setVisible(true);

and then realized that I had no apparent way of returning the result 
from the method! It has to wait for the user to interact with the dialog 
and select something, so I thought of figuring out some kind of 
wait/notify thing ... but then realized that hanging the EDT to wait for 
the user to input something will deadlock the app.

So it looks like I need to redesign to call select from a SwingWorker 
rather than from the EDT, or to have select return void and accept a 
callback object.

No problem, but this caused me to think about all the places littered 
through the existing code where I call the integer-returning 
JOptionPane.showOptionDialog(foo...) methods ...

I suppose those need to be moved to a SwingWorker too? Since the EDT 
will just sit in some event listener or another waiting for an int to be 
returned from the method, while the method waits for the EDT to produce 
an action event for the user clicking an option button in the dialog...

Funny that the Java Tutorial doesn't mention anything about the trio of 
non-void-returning JOptionPane static methods, the EDT, and concurrency, 
given that I don't see how they could be implemented not to deadlock or 
else blow chunks if called on the EDT.

5.Error *cannot return a value from method whose result type is void*

6. return array values from a method

7. [D&D] Dealing with exceptions and the return value in the importData-method of TransferHandler

8. this.selectedIndex returns a number, but this.value returns ""



Return to JAVA

 

Who is online

Users browsing this forum: No registered users and 52 guest