Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 returning multiple values
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ilovechicken




PostPosted: Tue May 17, 2005 7:43 pm   Post subject: returning multiple values

when i have written a method and want it to return multiple values to the method that called it how do i do it? the only way i know so far is in the method that is doing the calling write a line like;

variable = methodName ();

and in method name:

public boolean methodName ()
{
return true;
}

but this can only return one variable at a time...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue May 17, 2005 8:04 pm   Post subject: (No subject)

You either have to return an array, or some other collection or write a class yourself that wraps several values.

Something like:

Java:
class Pair<T1, T2>
{
   private T1 val1;
   private T2 val2;

   public Pair(T1 iv1, T2 iv2)
   {
      val1 = iv1;
      val2 = iv2;
   }

   public T1 first()
   {
      return val1;
   }

   public T2 second()
   {
      return val2;
   }
}


Convenient, isn't it?

:: silently points to all of the languages that *can* do this easily ::
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: