你可以这样做。净通过使用关键词"参考".是否有任何方式这样做Java?

有帮助吗?

解决方案

你在做什么在你的方法?如果你仅仅是填充现有阵列,然后你不需要通过引用的语义--无论。网或在Java。在这两种情况下,将参照过去了价值-所以改变的 对象 将可以通过调用者。就像在告诉人的地址你的房子,要求他们提供的东西了-没有问题。

如果你 真的 希望通过参考的语义,即呼叫者的会见所作的任何更改的参数本身,例如设置空或参照不同的字阵列,那么任何一种方法需要返回的新的价值,或者需要通过一个参照某种形式的"持有人",其中包含一个参考字阵列,并且其中可以(可能更改)的参考抓住从后面。

换句话说,如果你的方法看起来喜欢这样的:

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正在传递按值该死 <! / p>


下面是一个具体的例子:

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);
    }
}

此程序将创建长度byte8方法的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方法称为modifyArray方法和发送的通过使用复制的参照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
}

爪哇岛

次要模组要求:在 Counter.java 中使用 hashCode() 和 + 来连接字符串...

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  
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top