Solved

Copying arrays

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 50
Java Loop 6 59
difference of if loops 23 49
how to install java on RHEL image on EC2 4 26
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…
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 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:
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.

813 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

10 Experts available now in Live!

Get 1:1 Help Now