【问题标题】:Deep copying arrays in Java [duplicate]Java中的深度复制数组[重复]
【发布时间】:2016-03-31 14:32:40
【问题描述】:

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

这是我的代码:

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();
}

}

【问题讨论】:

  • java.lang.System.arraycopy()
  • 那些工作!谢谢!我将不得不制作多个数组,但这应该可以解决问题!

标签: java arrays sorting deep-copy


【解决方案1】:

你应该使用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 中,显然您可以给事物起更好的名称。

【讨论】:

  • 也许是一个小sn-p,显示它应该如何应用于OP的案例?
  • 添加了一个简短的例子
猜你喜欢
  • 2010-10-08
  • 2020-07-28
  • 2010-09-17
  • 2015-05-06
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 2011-07-25
  • 2012-07-03
相关资源
最近更新 更多