Solved

Copying arrays

Posted on 2004-04-14
4
233 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

911 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

16 Experts available now in Live!

Get 1:1 Help Now