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.
I'm creating a program that will time the length of time taken to execute various sorting algorithms. I'm using the System.currentTimeMillis()
any help would be much appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks a million
:-)
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].clo