如何在Java中使用等效的C ++引用参数? [英] How do I use an equivalent to C++ reference parameters in Java?
问题描述
假设我在C ++中有这个:
Suppose I have this in C++:
void test(int &i, int &j)
{
++i;
++j;
}
这些值在函数内部改变,然后在外部使用。我怎么能写一个在Java中做同样的代码?我想象我可以返回一个封装这两个值的类,但是这似乎很繁琐。
The values are altered inside the function and then used outside. How could I write a code that does the same in Java? I imagine I could return a class that encapsulates both values, but that seems really cumbersome.
推荐答案
Java没有等价的C ++引用。使这个工作的唯一方法是将值封装在另一个类中,并交换类中的值。
Java has no equivalent of C++ references. The only way to get this to work is to encapsulate the values in another class and swap the values within the class.
以下是有关此问题的冗长讨论: http: //www.yoda.arachsys.com/java/passing.html
Here is a lengthy discussion on the issue: http://www.yoda.arachsys.com/java/passing.html
这篇关于如何在Java中使用等效的C ++引用参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!