|
public class Copy1 { public static void main(String[] args) { double a = 3.14; // Original variable double b; b = a; // b is a copy of a System.out.println("a = " + a); System.out.println("b = " + b); b = b+10; // Update the copy must not change the original System.out.println("Values after updating the copy b:"); System.out.println("a = " + a); System.out.println("b = " + b); } } |
Output of this program:
a = 3.14 b = 3.14 Values after updating the copy b: a = 3.14 b = 13.14 |
How to run the program:
|
public class Copy1a { public static void main(String[] args) { double[] a = { 1.1, 2.2, 3.3 }; // The original array double[] b; // The copy b = a; // is b a copy of a ?? /* ==================================== Let's do an experiment.... ==================================== */ int i; /* ---------------------------------- Print the values before the update ---------------------------------- */ System.out.println("a array:"); for ( i = 0; i < a.length; i++) System.out.println( a[i] ); System.out.println("b array:"); for ( i = 0; i < b.length; i++) System.out.println( b[i] ); b[0] = b[0]+10; // Update the copy (must not change the original) /* ---------------------------------- Print the values after the update ---------------------------------- */ System.out.println("Values after updating the copy b[0]:"); System.out.println("a array:"); for ( i = 0; i < a.length; i++) System.out.println( a[i] ); System.out.println("b array:"); for ( i = 0; i < b.length; i++) System.out.println( b[i] ); } } |
How to run the program:
|
Output of this program:
a array: 1.1 2.2 3.3 b array: 1.1 2.2 3.3 Values after updating the copy b[0]: a array: 11.1 2.2 3.3 b array: 11.1 2.2 3.3 |
Observation (conclusion from this experiment):
|
$64,000 question:
|
The answer follows...
|
b = a; |
will copy the value of variable a (5000 in the example) into the variable b.
Result:
The names of the array elements accessed through the reference variable b are:
b[0] b[1] b[2] |
Specifically:
|
b[0] = b[0] + 10; |
The value in the array element a[0] is also updated
Because it is the same array element !!!
|
Example:
|
|
|
|
public class Copy2 { public static void main(String[] args) { double[] a = { 1.1, 2.2, 3.3 }; // The original array double[] b; // The copy b = new double[a.length]; // Create array of same length int i; for ( i = 0; i < a.length; i++ ) { b[i] = a[i]; // Copy each element in array ! } /* ==================================== Let's do the same experiment.... ==================================== */ /* ---------------------------------- Print the values before the update ---------------------------------- */ System.out.println("a array:"); for ( i = 0; i < a.length; i++) System.out.println( a[i] ); System.out.println("b array:"); for ( i = 0; i < b.length; i++) System.out.println( b[i] ); b[0] = b[0]+10; // Update the copy (must not change the original) /* ---------------------------------- Print the values after the update ---------------------------------- */ System.out.println("Values after updating the copy b[0]:"); System.out.println("a array:"); for ( i = 0; i < a.length; i++) System.out.println( a[i] ); System.out.println("b array:"); for ( i = 0; i < b.length; i++) System.out.println( b[i] ); } } |
Output of this program:
a array: 1.1 2.2 3.3 b array: 1.1 2.2 3.3 Values after updating the copy b[0]: a array: 1.1 2.2 3.3 b array: 11.1 2.2 3.3 |
Conclusion: array b is a independent and a true copy of the array b.
How to run the program:
|
|
Recall that the length of an array = the number of elements in the array.
|
Example programming languages with static arrays:
|
(But these languages does provide a special pointer arithmetic mechanism to implement dynamic arrays).
|
|
Notice that:
|
We will now change (update) the address stored inside the variable a and study its effects.
a = b ; // Put the value of variable b in variable a |
The result of this statement is illustrated in the following figure:
Explanation:
|
public class Copy3 { public static void main(String[] args) { double[] a = {5.1, 2.2, 3.4}; double[] b = {1.2, 4.6, 2.4, 8.1}; /* ------------------------------- Print array a before the change -------------------------------- */ System.out.println("a.length = " + a.length ); for ( int i = 0; i < a.length; i++ ) System.out.println( a[i] ); a = b; // a is now an alias for b /* ------------------------------- Print array a AFTER the change -------------------------------- */ System.out.println("a.length = " + a.length ); for ( int i = 0; i < a.length; i++ ) System.out.println( a[i] ); } } |
Output of this program:
a.length = 3 5.1 2.2 3.4 a.length = 4 1.2 4.6 2.4 8.1 |
You can witness the fact that a is an alias for b.
How to run the program:
|
|
Fact:
|
Pay attention to the array element highlighted wuth the cyan color:
Fact:
|
|
|
When this happens, the computer program will crash (= exits prematurely).
|
|
In other programming languages (such as C/C++), the programmer (that means you) is in fact responsible.
|
|
The steps of the solution in Java statements:
|
|
The following algorithm will increase the array size to twice the original size:
double[] a = { .... }; // The original array
double[] b; // help variable
/* -------------------------------------------------------
Array doubling algorithm: doubling the size of array a
------------------------------------------------------- */
b = new double[ 2*a.length ]; // Make an array twice the size of a
for ( int i=0; i < a.length; i++ ) // Copy elem's from old array to new
b[i] = a[i];
a = b; // Make a point to the new array
|