0

I have that:

      byte[] AllNumber = {4,3,2,1,0,5,6,7,8,9};
      byte[] MultNumber = {4,3,2,1,0,5,6,7,8,9}; // No matter the content
      byte[] DivNumber = {4,3,2,1,0,5,6,7,8,9}; // No matter the content

      Alter(AllNumber,MultNumber,DivNumber,5.0,3,2); //The Arrays must be Altered!!

      for (int i = 0; i<MultNumber.length; i++) {
        System.out.println("MultNumber["+i+"]:"+MultNumber[i]);
      }
      for (int i = 0; i<DivNumber.length; i++) {
        System.out.println("DivNumber["+i+"]:"+DivNumber[i]);
      }

Now I have this method:

      Alter(byte[] src, byte[] Mlt, byte[] Dvs, double dNum, int Lngt, int Ini) {
        // INI THE PROBLEM
        Mlt = Arrays.copyOf(Mlt, src.length + Lngt);  //HERE IS THE ASSIGNATION
        for (int i = ini; i<Mlt.length; i++) {
          Mlt[i] = Mlt[i]*dNum; //No matter the operation (to simplify the problem)
        }

        Dvs = Arrays.copyOf(Dvs, src.length - Lngt);  //HERE IS THE ASSIGNATION
        for (int i = Ini; i<Dvs.length; i++) {
          Dvs[i] = Dvs[i]/dNum; //No matter the operation (to simplify the problem)
        }
        // END THE PROBLEM
      }

Another Attempt

      //Another Attempt!!! 
      Alter(byte[] src, byte[] Mlt, byte[] Dvs, double dNum, int Lngt, int Ini) {
        // INI THE PROBLEM
        byte[] TM = new byte[src.length + Lngt]
        for (int i = ini; i<Mlt.length; i++) {
          TM[i] = Mlt[i]*dNum; //No matter the operation (to simplify the problem)
        }
        Mlt = TM;  //HERE IS THE ASSIGNATION
        TM = null;

        byte[] TD = new byte[src.length - Lngt]
        for (int i = Ini; i<Dvs.length; i++) {
          TD[i] = Dvs[i]/dNum; //No matter the operation (to simplify the problem)
        }
        Dvs = TD;  //HERE IS THE ASSIGNATION
        TD = null;
        // END THE PROBLEM
      }

I want to get two arrangements changed after performing the call of method "Alter". How I can do it this?

I need to change the length of Arrays!

Thank you for your valuable help.

PD. It seems later to make the assignations to the arrays, the "Call by Reference" is converted in "Call by Value". If THE ASSIGNATION is omitted the "Call by Reference" follows being.

4

1 回答 1

2

我需要改变数组的长度!

这在 Java 中是不可能的。您可以使用动态数据结构,例如java.util.List接口的实现。

您的分配没有帮助,因为方法参数是局部变量,引用原始对象。所以你的对象有两个引用,你只改变你方法中已知的引用。

Java 使用按值调用,对于引用数据类型,值是引用的值(因此您获得对同一对象的引用)。

当你想改变一个数组时,你可以做这样的事情。

public static int[] arrayTwiceAsBig(int[] original) {
    int[] newOne = new int[original.length * 2);
    System.arraycopy(original, 0, newOne, 0, original.length);
    return newOne;
}

并这样称呼它:

int[] myArray = {1,2,3};
myArray = arrayTwiceAsBig(myArray);
System.out.println(Arrays.toString(myArray));
于 2012-12-10T15:51:13.967 回答