Copying arrays/assigning the 'value' of the array to another.

  • Thread starter Thread starter Hiche
  • Start date Start date
  • Tags Tags
    Array Value
AI Thread Summary
The code snippet provided attempts to copy an array in Java, but it contains an error. The line "aCopy = a;" does not copy the elements of array 'a' into 'aCopy'; instead, it assigns the reference of 'a' to 'aCopy', meaning both variables point to the same array. To correctly copy the elements, a loop should be used to assign each element individually, or the `System.arraycopy()` method can be utilized for a more efficient approach. The discussion emphasizes the need for proper array copying techniques to ensure that changes to one array do not affect the other.
Hiche
Messages
82
Reaction score
0
Code:
int[] aCopy = new int[a.length];
for (int i = 0; i < a.length; i++)
	aCopy[i] = a[i];

Is this the way to 'copy' an array? Basically, I want the elements inside the the array a to be copied or assigned to the array aCopy.
 
Technology news on Phys.org
This should work. Try it out and see.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top