Java:通过引用传递int的最佳方法 [英] Java : Best way to pass int by reference
问题描述
我有一个解析函数,它解析来自字节缓冲区的编码长度,它将解析的长度作为int返回,并将索引作为整数arg作为缓冲区。我希望函数根据它的解析来更新索引,即希望通过引用传递该索引。在C中,我只传递 int *
。
在Java中用最干净的方法是什么?
我目前正在考虑传递索引arg。作为 int []
,但它有点难看。
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.
推荐答案
您可以尝试使用Apache Commons库中的 org.apache.commons.lang.mutable.MutableInt
。在语言本身没有直接的方法。
You can try using org.apache.commons.lang.mutable.MutableInt
from Apache Commons library. There is no direct way of doing this in the language itself.
这篇关于Java:通过引用传递int的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!