?
Solved

Copying arrays

Posted on 2004-04-14
4
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 2000 total points
ID: 10825066
Hi Paul_Murphy,

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

Expert Comment

by:Webstorm
ID: 10825116
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
ID: 10825130
thanks a million
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10825186
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.
Suggested Courses

719 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