Java:通过引用传递 int 的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3326112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:11:13  来源:igfitidea点击:

Java : Best way to pass int by reference

javapass-by-reference

提问by fred basset

I have a parsing function that parses an encoded length from a byte buffer, it returns the parsed length as an int, and takes an index into the buffer as an integer arg. I want the function to update the index according to what it's parsed, i.e. want to pass that index by reference. In C I'd just pass an int *. What's the cleanest way to do this in Java? I'm currently looking at passing the index arg. as an int[], but it's a bit ugly.

我有一个解析函数,可以解析字节缓冲区中的编码长度,它将解析后的长度作为 int 返回,并将索引作为整数 arg 传入缓冲区。我希望函数根据解析的内容更新索引,即希望通过引用传递该索引。在 C 中,我只是通过一个int *. 在 Java 中最干净的方法是什么?我目前正在考虑传递索引 arg。作为int[],但它有点丑陋。

采纳答案by doublep

You can try using org.apache.commons.lang.mutable.MutableIntfrom Apache Commons library. There is no direct way of doing this in the language itself.

您可以尝试使用org.apache.commons.lang.mutable.MutableIntApache Commons 库。在语言本身中没有直接的方法来做到这一点。

回答by Yuval Adam

You cannot pass arguments by reference in Java.

在 Java 中不能通过引用传递参数。

What you can do is wrap your integer value in a mutable object. Using Apache Commons' MutableIntis a good option. Another, slightly more obfuscated way, is to use an int[]like you suggested. I wouldn't use it as it is unclear as to why you are wrapping an intin a single-celled array.

您可以做的是将整数值包装在一个可变对象中。使用 Apache Commons'MutableInt是一个不错的选择。另一种稍微混淆的方法是使用int[]您建议的类似方法。我不会使用它,因为不清楚为什么要将 an 包装int在单细胞数组中。

Note that java.lang.Integeris immutable.

请注意,这java.lang.Integer是不可变的。

回答by mikej

This isn't possible in Java. As you've suggested one way is to pass an int[]. Another would be do have a little class e.g. IntHolderthat wrapped an int.

这在 Java 中是不可能的。正如您所建议的那样,一种方法是通过int[]. 另一个是有一个小类,例如IntHolder包装一个int.

回答by John Kugelman

Wrap the byte buffer and index into a ByteBufferobject. A ByteBuffer encapsulates the concept of a buffer+position and allows you to read and write from the indexed position, which it updates as you go along.

将字节缓冲区和索引包装到一个ByteBuffer对象中。ByteBuffer 封装了缓冲区+位置的概念,并允许您从索引位置读取和写入,它会随着您的进行而更新。

回答by Anoos Sb

You can design new class like this:

您可以像这样设计新类:

public class Inte{
       public int x=0;
}

later you can create object of this class :

稍后您可以创建此类的对象:

Inte inte=new Inte();

then you can pass inteas argument where you want to pass an integer variable:

然后您可以inte在要传递整数变量的地方作为参数传递:

public void function(Inte inte) {
some code
}

so for update the integer value:

所以要更新整数值:

inte.x=value;

for getting value:

获取价值:

Variable=inte.x;

回答by user3070377

You can use java.util.concurrent.atomic.AtomicInteger.

您可以使用java.util.concurrent.atomic.AtomicInteger.

回答by Mickael Bergeron Néron

You can create a Reference class to wrap primitives:

您可以创建一个 Reference 类来包装原语:

public class Ref<T>
{
    public T Value;

    public Ref(T value)
    {
        Value = value;
    }
}

Then you can create functions that take a Reference as a parameters:

然后您可以创建将引用作为参数的函数:

public class Utils
{
    public static <T> void Swap(Ref<T> t1, Ref<T> t2)
    {
        T temp = t1.Value;
        t1.Value = t2.Value;
        t2.Value = temp;
    }
}

Usage:

用法:

Ref<Integer> x = 2;
Ref<Integer> y = 9;
Utils.Swap(x, y);

System.out.println("x is now equal to " + x.Value + " and y is now equal to " + y.Value";
// Will print: x is now equal to 9 and y is now equal to 2

Hope this helps.

希望这可以帮助。