gpt4 book ai didi

java - Java中的深拷贝数组

转载 作者:行者123 更新时间:2023-12-02 03:45:40 25 4
gpt4 key购买 nike

我正在编写一个程序,通过创建随机整数数组并使用五种不同的算法对其进行排序来计算不同排序算法的运行时间。我需要对原始数组进行深层复制,以便当我运行下一个排序算法时,它不会对已经排序的数组进行排序(从而影响我的测试结果)。

这是我的代码:

import java.io.*;
import java.util.*;

class SortingTest
{
static int runtime=0;
static int start=0;
static int stop=0;

/**@param start the time when the algorithm started sorting
@param stop the time when the algorithm finished sorting
@return runtime the total time it took for the sorting algorithm to sort
a function that calculates the runtime of the sorting algorithm*/
public static int findruntime(int start, int stop)
{
return runtime=stop-start;
}

/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs bubble sort and prints the run time*/
public static void runbubble(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.bubble(A);
stop=(int)System.currentTimeMillis();

if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}

findruntime(start,stop);
System.out.println("The running time for Bubble Sort is "+ runtime+ " milliseconds.");
}

/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs selection sort and prints the run time*/
public static void runselection(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.select(A);
stop=(int)System.currentTimeMillis();

if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}

findruntime(start,stop);
System.out.println("The running time for Selection Sort is "+ runtime+" milliseconds.");
}

/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs insertion sort and prints the run time*/
public static void runinsertion(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.insertion(A);
stop=(int)System.currentTimeMillis();

if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}

findruntime(start,stop);
System.out.println("The running time for Insertion Sort is "+ runtime+" milliseconds.");
}

/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs quick sort and prints the run time*/
public static void runquick(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.quick(A);
stop=(int)System.currentTimeMillis();

if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}

findruntime(start,stop);
System.out.println("The running time for Quick Sort is "+ runtime+" milliseconds.");
}

/**@param A the array containing the numbers to be sorted
@param n the length of the array
a function that runs shell sort and prints the run time*/
public static void runshell(Integer [] A, int n)
{
start=(int)System.currentTimeMillis();
Sorts.shell(A);
stop=(int)System.currentTimeMillis();

if (n<=100)
{
for(int i=0; i<A.length; i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
}

findruntime(start,stop);
System.out.println("The running time for Shell Sort is "+ runtime+" milliseconds.");
}

public static void main (String [] args)
throws IOException
{
System.out.println("Welcome to the Sorting Algorithms Speed Test!");
System.out.print("Please enter a value for n:");

Scanner cin = new Scanner(System.in);
int n= cin.nextInt();

//create array of size n
Integer[] list1= new Integer[n];

//generate and fill in the array
for(int i=0; i<list1.length; i++)
{
int number= (int)(1+n*Math.random());
list1[i]=number;
}

//print if n<=100
if (n<=100)
{
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();
}
// MUST FIGURE OUT A WAY SO THAT THE ORIGINAL ARRAY LIST DOES NOT GET SORTED AS WELL

//using bubble sort
/*runbubble(list1,n);


//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();

//using selection sort
runselection(list1,n);

//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();

//using insertion sort
runinsertion(list1,n);

//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();

//using quick sort
runquick(list1,n);

//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();*/

//using shell sort
runshell(list1,n);

//TESTING ONLY
System.out.println("This is to make sure that the original array was never tampered with...");
for(int i=0; i<list1.length; i++)
{
System.out.print(list1[i]+" ");
}
System.out.println();
}

}

最佳答案

您应该使用System.arrayCopy()

所以当你声明你的整数数组时

//create array of size n
Integer[] list1 = new Integer[n];
Integer[] copyList1 = new Integer[n];

因此,您将创建 2 个数组,然后在使用可以调用的值填充 list1 后

System.arraycopy(list1 ,0 ,copyList1 ,0 ,list1.length);

它将把list1中的所有值复制到copyList1中,显然你可以给东西更好的名字。

关于java - Java中的深拷贝数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36336388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com