Now tell replicate and from an array of aliases do?

Many beginners can not tell the difference between a copy and play an array of aliases, or difficult to detect, and often confused. Now we look at.

1, first look at the array copy the code a:

int N=a.length;
double[] b=new double[N];
for(int i=0;i<N;i++)
    b[i]=a[i];

2, then look to the code array from the alias b:

int[] a=new int[N];
...
a[i]=1234;
...
int[] b=a;
...
b[i]=5678;  //a[i]的值也会变成5678

3 to fully understand them


  • For a, once the declaration creates an array, its size is fixed. The program can be obtained by a.length array a [] of length, but it is always the last element a [a.length-1]. Then the original values ​​of the array elements one by one to the new array.

  • For b, the array name represents the entire array ------ If we are given an array variable to another variable, then the two variables point to the same array.

  • and so:
    The key: a the double [] b = new double [N]; statement. It creates a new array.

Released nine original articles · won praise 18 · views 1046

Guess you like

Origin blog.csdn.net/qq_44222849/article/details/105314549