How do I use an equivalent to C++ reference parameters in Java?

后端 未结 11 1336
借酒劲吻你
借酒劲吻你 2020-12-09 14:33

Suppose I have this in C++:

void test(int &i, int &j)
{
    ++i;
    ++j;
}

The values are altered inside the function and then use

11条回答
  •  孤街浪徒
    2020-12-09 14:51

    Well, there are a couple of workarounds. You mentioned one yourself. Another one would be:

    public void test(int[] values) {
        ++values[0];
        ++values[1];
    }
    

    I would go with the custom object, though. It’s a much cleaner way. Also, try to re-arrange your problem so that a single method doesn’t need to return two values.

提交回复
热议问题