简体   繁体   English

如何让方法返回多个对象?

[英]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 than one object? 但是,有一种方法可以返回多个对象的方法吗? Like say, 2 Strings? 比如说2个弦?

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. 如果您希望返回不同类型的对象,则返回对象的ArrayList。

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

It is not possible in Java. 在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. FunctionalJava为产品提供了一个方便的P类,可以简化此过程。 http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/P.html 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: 一种方法是将输出对象传递给您的方法,例如以arraylist的形式,因此它将类似于:

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. 当然,必须在方法之外创建输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM