开发者

How to let method return more than one object?

This is probably a very stupid question. But is there a way that a method could return more 开发者_如何学Cthan one object? Like say, 2 Strings?

public static String multipleReturnMethod(){

   String a = "a";
   String b = "b";

   return a;
   return b;
}

Possible? Not Possible? How do you make it possible?


You could return an array...

public static String[] multipleReturnMethod(){    
   String a = "a";
   String b = "b";
   return new String[] { a, b };
}

Or a list, or a type which encapsulates the two values, etc...

You can only return a single value from a method, but that value may itself give access to multiple "subvalues".

If you can give more information about what you're trying to do, we may be able to help more.


Not possible without chaning the return object:

  1. Either create a class for it:

    class Strings {
        String a, b;
        Strings(String a, String b) {
            this.a = a;
            this.b = b;
        }
    }
    
    public static Strings multipleReturnMethod(){
         return new Strings("a", "b");
    }
    
  2. Or return an array

    public static String[] multipleReturnMethod(){
        return new String[] { a, b };
    }
    


If you want to keep the signature, you could concatenate the strings with a defined delimiter:

public static final String DELIMITER = "\t";  // or something else 
                                              // not used in strings

public static String multipleReturnMethod(){

   String a = "a";
   String b = "b";

   return a + DELIMITER + b;
}

(although I prefer changeing the signature and returning an array or a collection of values, see Jon's answer)


If they are all the same type of object then the simplest way is to just return an array of that type of object

public static String[] methodA(){
  String[] temp = new String[5];
  // do work
  return temp;
}

If you wish to return different types of objects then return an ArrayList of objects.

public static ArrayList<Object> methodB(){
  ArrayList<Object> temp = new ArrayList<Object>();
  // do work
  return temp;
}


It is not possible in Java. You must create a wrapper class around the elements you want to return and return that single element and contains everything you want to return.

FunctionalJava has a handy class P for Product that makes this easy. http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/P.html

return P.p(a,b);


It's impossible to return more then one object, never!. Solution is to return an object wich contains the objects you want to return.


If you have a more complex example, you can use a listener pattern

interface ResultListener {
   void resultOne(String text);
   void resultTwo(String text, int number);
}

public static void multipleReturnMethod(ResultListener result){
   result.resultOne("a");
   result.resultOne("b");
   result.resultTwo("C", 1);
}

As you can see you can return any combination of results to the caller with a mix or combination of types.


One way is to pass an output object to your method, for example in the form of an arraylist, so it will be something like:

public static ArrayList<String> multipleReturnMethod(ArrayList<String> output) {

 String a = "a";
 String b = "b";

 output.add(a);
 output.add(b);
 return output; //not really necessary
}

Of course output has to be created outside your method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜