您可以使用关键字“ref”在 .NET 中执行此操作。Java有什么办法吗?
3 回答
你在你的方法中做什么?如果您只是填充现有数组,那么您不需要传递引用语义 - 无论是在 .NET 中还是在 Java 中。在这两种情况下,引用都将通过值传递 - 因此调用者可以看到对对象的更改。这就像告诉某人你家的地址,并要求他们送东西给它——没问题。
如果您真的想要传递引用语义,即调用者将看到对参数本身所做的任何更改,例如将其设置为 null 或对不同字节数组的引用,那么任一方法都需要返回新值,或者您需要传递对某种“持有人”的引用,该持有人包含对字节数组的引用,并且稍后可以从中获取(可能已更改的)引用。
换句话说,如果你的方法看起来像这样:
public void doSomething(byte[] data)
{
for (int i=0; i < data.length; i++)
{
data[i] = (byte) i;
}
}
那你就没事了。如果您的方法如下所示:
public void createArray(byte[] data, int length)
{
// Eek! Change to parameter won't get seen by caller
data = new byte[length];
for (int i=0; i < data.length; i++)
{
data[i] = (byte) i;
}
}
那么您需要将其更改为:
public byte[] createArray(int length)
{
byte[] data = new byte[length];
for (int i=0; i < data.length; i++)
{
data[i] = (byte) i;
}
return data;
}
或者:
public class Holder<T>
{
public T value; // Use a property in real code!
}
public void createArray(Holder<byte[]> holder, int length)
{
holder.value = new byte[length];
for (int i=0; i < length; i++)
{
holder.value[i] = (byte) i;
}
}
有关详细信息,请阅读C# 中的参数传递和 Java中的参数传递。(恐怕前者比后者写得好。总有一天我会抽空做一个更新。)
实际上,在 Java 中,引用是按值传递的。
在这种情况下,引用是一个byte[]
对象。任何影响对象本身的更改都将从调用者方法中看到。
但是,如果您尝试替换引用,例如使用 new byte[length]
,您只是替换了通过值传递获得的引用,因此您不会更改调用方方法中的引用。
这里有一篇关于这个问题的有趣读物:Java is Pass-by-Value Dammit!
这是一个具体的例子:
public class PassByValue
{
public static void modifyArray(byte[] array)
{
System.out.println("Method Entry: Length: " + array.length);
array = new byte[16];
System.out.println("Method Exit: Length: " + array.length);
}
public static void main(String[] args)
{
byte[] array = new byte[8];
System.out.println("Before Method: Length: " + array.length);
modifyArray(array);
System.out.println("After Method: Length: " + array.length);
}
}
该程序将在方法中创建一个byte
长度数组,8
该main
方法将调用该modifyArray
方法,在该方法中创建一个新byte
的长度数组16
。
看起来通过在方法中创建一个新byte
数组,返回该方法时数组modifyArray
的长度将是,但是,运行这个程序会发现一些不同的东西:byte
main
16
Before Method: Length: 8
Method Entry: Length: 8
Method Exit: Length: 16
After Method: Length: 8
从该方法返回byte
时数组的长度恢复为,而不是。modifyArray
8
16
这是为什么?
那是因为该main
方法调用了该方法并使用pass-by-valuemodifyArray
发送了一个复制的引用new byte[8]
。然后,该方法通过创建一个. 当我们离开时,对 的引用超出了范围(最终将被垃圾回收。)但是,该方法仍然具有对的引用,因为它只发送了复制的引用,而不是对引用的实际引用。modifyArray
new byte[16]
modifyArray
new byte[16]
main
new byte[8]
这应该证明 Java 将使用按值传递来传递引用。
Java 对方法参数使用按值传递。
- 基元(int、boolean 等)是 Java 中的特例……不是对象本身。在这种情况下,原始(参数)的副本被传递到函数中。这与价值传递理论非常吻合。
- 对于对象,发生的情况是对象的引用是按值传递的(生成引用的副本而不是对象)......但是两个引用都指向同一个对象。因此,如果您在方法中修改对象参数,则实际对象将被修改。
这篇文章应该可以帮助你.. http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
至于OP的问题,只需将对 byte[] 数组的引用传递给方法。最终结果将类似于通过引用传递。如果您修改字节数组,调用者将能够看到方法执行后的更改。
更新以平息阻力:) => 表示输出
.NET 土地
class Counter
{
private int m_count = 0;
public override string ToString()
{
return String.Format("Counter ID{0} : Value {1}", this.GetHashCode(), m_count);
}
public void Increment()
{ m_count++; }
}
class MakeAPass
{
public void PassByValueAndModify(int i)
{ i = 20; }
public void PassByRefAndModify(ref int i)
{ i = 20; }
public void PassByValueAndModify(Counter c)
{ c.Increment(); }
public void PassByRefAndModify(ref Counter c)
{ c.Increment(); }
public void PassByRefAndReassign(ref Counter c)
{
c = new Counter();
for (int i=0; i<5; ++i)
c.Increment();
}
}
static void Main(string[] args)
{
MakeAPass obj = new MakeAPass();
int intVal = 10;
obj.PassByValueAndModify(intVal);
Console.WriteLine(intVal); // => 10
obj.PassByRefAndModify(ref intVal);
Console.WriteLine(intVal); // => 20
Counter obCounter = new Counter();
obj.PassByValueAndModify(obCounter);
Console.WriteLine(obCounter.ToString()); // => Counter ID58225482 : Value 1
obj.PassByRefAndModify(ref obCounter);
Console.WriteLine(obCounter.ToString()); // => Counter ID58225482 : Value 2
obj.PassByRefAndReassign(ref obCounter);
Console.WriteLine(obCounter.ToString()); // => Counter ID54267293 : Value 5
}
爪哇地
Minor mods reqd:使用 hashCode() 和 + 在 Counter.java 中连接字符串...
class MakeAPass
{
public void PassByValueAndModify(int i)
{ i = 20; }
// can't be done.. Use Integer class which wraps primitive
//public void PassByRefAndModify(ref int i)
public void PassByValueAndModify(Counter c)
{ c.Increment(); }
// same as above. no ref keyword though
//public void PassByRefAndModify(ref Counter c)
// this can't be done as in .net
//public void PassByRefAndReassign(ref Counter c)
public void PassAndReassign(Counter c)
{
c = new Counter();
for (int i=0; i<5; ++i)
c.Increment();
}
}
public static void main(String args[])
{
MakeAPass obj = new MakeAPass();
int intVal = 10;
obj.PassByValueAndModify(intVal);
System.out.println(intVal); // => 10
//obj.PassByRefAndModify(ref intVal);
//System.out.println(intVal); // can't get it to say 20
Counter obCounter = new Counter();
obj.PassByValueAndModify(obCounter);
System.out.println(obCounter.ToString()); // => Counter ID3541984 : Value 1
//obj.PassByRefAndModify(ref obCounter);
//Console.WriteLine(obCounter.ToString()); // no ref. but can make it 2 by repeating prev call
obj.PassAndReassign(obCounter);
System.out.println(obCounter.ToString()); // => Counter ID3541984 : Value 1
// can't get it to say 5
}