开发者

How to return two or more objects in one method?

开发者 https://www.devze.com 2023-02-14 13:48 出处:网络
Lets say you want a method to return both a generated object and a boolean indicating the success or failure of doing so.

Lets say you want a method to return both a generated object and a boolean indicating the success or failure of doing so.

In some languages, like C, you might have a function return additional objects by having reference parameters, but you cannot do this in Java since Java is "pass-by-value", so how do you return several objects in 开发者_StackOverflow社区Java?


You could do

class Result{

    Result(boolean result, Object value)
    {
        this.result = result;
        this.value = value;
    }

    public boolean getResult()
    { 
         return result; 
    }


    public Object getValue()
    { 
       result value; 
    }
    private boolean result;
    private Object value;

}

and have your function return an instance of Result

private Result myMethod()
{
   boolean result = doStuff();
   Object value = getValue();
   return new Result(result, value)
}


I would suggest one of two things, either return null and check for it wherever you need that object, or throw an exception in the event of an error.

On success just return the object.


Option 1: return one of these:

class ReturnValue {
  boolean isSuccessful;
  Object returnValue;
}

Option 2:

Object yourMethod() throws YourException {
}

Choose Option 1 is it's quite reasonable for the method to fail. Choose option 2 if the method should generally work, and the cases when it doesn't are indeed exceptional.


Pass a 1-element array to the method and get the result back in the array:

public boolean method(Object[] arg) {
    // ...
    arg[0] = result;
    return success;
}


How about creating a class for holding two objects:

public class Tuple<A,B>{
  A first;
  B second;
}

Then your method can be:

public Tuple<Boolean, Object> myMethod(){
    .
    .
    .
    Tuple<Boolean, Object> result = new Tuple<Boolean, Object>();
    result.first = success;
    result.second = generatedObject;
    return result;
}


Use a bean as the return type

public class Result
{
private boolean error = false;
private Object result = null; 
public Result(boolean error, Object result){ this.error=error; this.result=result;}

public boolean isError(){ return this.error; }
public Object getResult(){ return this.result; }
}

then your function becomes

public Result myFunc()
{
   //do stuff here
   Result r = new Result(false,retObj);

   return r;
}

Calling code does

Result result = myFunc();
if(result == null || result.isError())

{
//handle error
}
else
{
   Object actualResult = result.getResult();
   //process actualResult
}

You may use generics to pass any kind of class so that you may declare Result<TypeName> that returns TypeName rather than Object


http://www.osnews.com/story/20076/Multiple_Return_Values_in_Java

is that helpful?

you should make your own class...


I would suggest to create a container object which will have a return code and an object. Alternatively, it could return null or an array of objects.

0

精彩评论

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