How to return multiple integers in java?

I have a method that returns an error code (int) and a result (int). If the error code is 0, the result is valid. In C ++, it might look like this:

int method(int& result);

Java calls by reference, so if we change an object in a method, the change is displayed as the caller. But int is not a class, so I cannot do this in java:

int method(int result);

Both Integer and Long are immutable in Java, so the change is not displayed by the caller if I do it this way:

Integer method(Integer result);

Wrap the result in the Wrapper class! But, it is not so simple!

I have been working with C ++ for a long time and am moving to java, it really bothers me! can anyone provide a solution?

===================================

Well, in conclusion, there are these current solutions:

  • pass array as parameter
  • pass wrapper as parameter
  • return a couple
  • ,
  • mutableint

1- 2- api 3- 4- 5-, , 6- ,

!

+5
11
0

: ; . .

+4

. , :

  • 2 ( );

  • ( );

  • <int 1>,<int 2>. split(), .

  • .

+3

, . method , , . Java-esque. ++ ( , ).

C-system-call-ish. , .

- , , ( "templated" ) TwoTuple<TYPE1, TYPE2>, . , Java , .:)

+2

, .

public class ResultData {
    private int result;
    private int errorCode;

    public ResultData(int errorCode, int result) {
        this.result = result;
        this.errorCode = errorCode;
    }

    // Getters...
}

:

public ResultData method() {
    // Do stuff
    return new ResultData(error, result);
}

, , , , , , , , . , , , .

+1

: 1. , ( ). , indexOf() java ArrayList -1, , . 2. . , - , , 0 .

.

+1

, :

int method(int[] result) {
   result[0] = 1; // set result[0] as your out value
   // etc.
}

" " int.

0

​​Java. ... ...

List<Integer> method(Parameter... paramteters)
{
   List<Integer> listOfValues = ...

   //some calculations

   if(errorCondition) throw new SomeException();

   //more calculations, if needed       

   return listOfValues;
}
0

, , . ++.

.


, .

, , -

 interface ErrorOrResult {
    void onError(int error);
    void onResult(int result);
 }

 a.method(errorOrResultImpl); // can use an anonymous implementation here.

, .


. .

, , . - int[], .

 int[] results = { 0, 0 };

 a.method(results);

.

 long result = a.method();
 // split into two int values.

.

 int error = a.method();
 int result = a.methodResult();

, Collections.binarySearch,

 int errorOrResult = a.method();
 if (errorOresult < 0)
    int error = ~errorOrResult;
 else
    int result = errorOrResult;

, , , .

0

, , . commons-lang v3 Pair

Pair<Integer, Integer> method(){
   return Pair.of(a,b)
}
0

-, int? Java. ( , ). / , - . , [2004] .

, . AtomicInteger . !

(, ) . Pair<,> . ( .)

A more exotic way is to pass a callback object. this is especially useful if the result data differs depending on the possible types of results. Ask the method to return void(or possibly the result of the callback) and add the parameter. Call the method depending on the type of result. Add as many arguments (within taste) and methods as possible. A typical caller will use an anonymous inner class and should not include an error code. Of course, returning to the included method again causes a data retrieval problem.

0
source

All Articles