传递方法时数组的副本不会更改

时间:2014-05-22 00:57:16

标签: java arrays copying

我正在尝试使用我在同一个数组上创建的4种不同的方法,但是当我通过第二种方法传递原始数组的副本时,它会打印出在原始数组通过第一种方法后打印的相同数组方法

如果原始数组通过它们,两种方法都可以正常工作。

public class ArrayPractice
{

    static double[] values = new double[11]; 
    Random randomInt = new Random();         //apparently main can't use nonstatic     field values

    public static void main(String[] args)
    {
        //when methods are created run them each once for the array we created and     then print out the result.
        ArrayPractice AP = new ArrayPractice();

        AP.fillArray(values);
        double[] valuesCopyOne = Arrays.copyOf(values, values.length); //use this     to run the original array through the methods rather than the modified array
        AP.printArray(values);
        System.out.println();

        AP.firstLastSwap(values);
        AP.printArray(values);
        System.out.println();

        AP.shiftElementsLeft(valuesCopyOne); //deal with using the copy after this     method is fixed
        AP.printArray(valuesCopyOne);
        System.out.println();

    }

0 个答案:

没有答案