Solved

Copying arrays

Posted on 2004-04-14
4
232 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:Paul_Murphy
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 500 total points
Comment Utility
Hi Paul_Murphy,

   int[] newArray = (int[]) oldArray.clone();
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
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();

0
 

Author Comment

by:Paul_Murphy
Comment Utility
thanks a million
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
:-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now