Java 中的深度复制数组
Deep copying arrays in Java
我正在编写一个程序,通过创建一个随机整数数组并使用五种不同的算法对其进行排序,对不同排序算法进行 运行 次排序。我需要对原始数组进行深度复制,这样当我 运行 下一个排序算法时,它不会对已经排序的数组进行排序(从而影响我的测试结果)。
这是我的代码:
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,显然你可以给东西起更好的名字。
我正在编写一个程序,通过创建一个随机整数数组并使用五种不同的算法对其进行排序,对不同排序算法进行 运行 次排序。我需要对原始数组进行深度复制,这样当我 运行 下一个排序算法时,它不会对已经排序的数组进行排序(从而影响我的测试结果)。
这是我的代码:
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,显然你可以给东西起更好的名字。