简体   繁体   English

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

[英]Java : Best way to pass int by reference

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. 我有一个解析函数,它解析来自字节缓冲区的编码长度,它将解析的长度作为int返回,并将索引作为整数arg作为缓冲区。 I want the function to update the index according to what it's parsed, ie want to pass that index by reference. 我希望函数根据它的解析来更新索引,即希望通过引用传递该索引。 In C I'd just pass an int * . 在C中,我只传递一个int * What's the cleanest way to do this in Java? 在Java中最干净的方法是什么? I'm currently looking at passing the index arg. 我目前正在考虑传递索引arg。 as an int[] , but it's a bit ugly. 作为int[] ,但它有点难看。

You can try using org.apache.commons.lang.mutable.MutableInt from Apache Commons library. 您可以尝试使用Apache Commons库中的org.apache.commons.lang.mutable.MutableInt There is no direct way of doing this in the language itself. 在语言本身没有直接的方法。

This isn't possible in Java. 这在Java中是不可能的。 As you've suggested one way is to pass an int[] . 正如您所建议的那样,一种方法是传递int[] Another would be do have a little class eg IntHolder that wrapped an int . 另一个会有一个小类,例如包含int IntHolder

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' MutableInt is a good option. 使用Apache Commons的MutableInt是一个不错的选择。 Another, slightly more obfuscated way, is to use an int[] like you suggested. 另一种稍微混淆的方式是使用你建议的int[] I wouldn't use it as it is unclear as to why you are wrapping an int in a single-celled array. 我不会使用它,因为不清楚为什么要在单细胞阵列中包装int

Note that java.lang.Integer is immutable. 请注意, java.lang.Integer是不可变的。

Wrap the byte buffer and index into a ByteBuffer object. 将字节缓冲区和索引包装到ByteBuffer对象中。 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封装了缓冲区+位置的概念,允许您从索引位置读取和写入,随着时间的推移,它会更新。

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

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 inte as 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;

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: 然后,您可以创建将Reference作为参数的函数:

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM