Link to home
Start Free TrialLog in
Avatar of Paul_Murphy
Paul_Murphy

asked on

Copying arrays

i need a little bit of help here - i'll try to explain it as best as i can.

I'm creating a program that will time the length of time taken to execute various sorting algorithms.  I'm using the System.currentTimeMillis() before and after tha algorithm to time it.  However, since most of the algorithms run really fast, i have to execute the loop many times getting the time at the start and at the end and then getting the average.  However, I need to reset the values of the array back to the unsorted values each time.  When i use something like newArray = oldArray, the values in oldArray automatically get replaced with the sorted values and i end up sorting the already sorted values.  Is there any way i can copy the values of the array without it changing the values of the original.  I cant use a for loop and do the values one at a time because this would then affect the running time of the algorithm that i am trying to time.

any help would be much appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Webstorm
Webstorm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Webstorm
Webstorm

You can also replace int[] by another array type.


If you need (for your problem, i don't think you need) also to duplicate array values (array elements type which are not primitive type (short, char, int, boolean, long) like objects and array, you can duplicate each element :

Cloneable[] newArray = (Cloneable[]) oldArray.clone();
for (int i=0;i<newArray.length;i++)
    newArray[i] = (Cloneable)newArray[i].clone();

Avatar of Paul_Murphy

ASKER

thanks a million