Java - variable in method not removed after call

I really need to explain the behavior of the code I created. Variable preyPred here: private static double[] preyPredLV(double[] preyPred, double[] a, double[] b, int n)

is it local to the preyPredLV method or not?

Because when I manipulate it during a method call and then call the method again for different ns, it doesn't assign preyPredDefault as the preyPred value, but uses its own value from the previous call. Should its previous value be discarded when the method returns an output value and should not be the new value assigned on the next call? Can someone please explain? Thanks you

All code:

public class Main {

        public static void main(String[] args) {
            double[] preyPredDefault = {300, 20};
            double[] a = {0.1, 0.01};
            double[] b = {0.01, 0.00002};

            int n = 1;
            double[] result = preyPredLV(preyPredDefault, a, b, n);
            System.out.println("After "+n+" generations: Hares = "+result[0]+" and Lynx = "+result[1]);


            n = 2;
            result = preyPredLV(preyPredDefault, a, b, n);
            System.out.println("After "+n+" generations: Hares = "+result[0]+" and Lynx = "+result[1]);

        }

        private static double[] preyPredLV(double[] preyPred, double[] a, double[] b, int n) {
            double[] preyPredOld = new double[2];
            System.out.println("n = "+n);
            System.out.println("preyPred[0] = "+preyPred[0]);
            System.out.println("preyPred[1] = "+preyPred[1]);
            for(int iteration = 0; iteration < n; iteration++){
                preyPredOld[0] = preyPred[0];
                preyPredOld[1] = preyPred[1];
                preyPred[0] = preyPredOld[0] * (1 + a[0] - a[1]*preyPredOld[1]);
                preyPred[1] = preyPredOld[1] * (1 - b[0] + b[1]*preyPredOld[0]);
            }
            return preyPred;
        }
    }

      

Result:

n = 1
preyPred[0] = 300.0
preyPred[1] = 20.0
After 1 generations: Hares = 270.00000000000006 and Lynx = 19.92
n = 2
preyPred[0] = 270.00000000000006 
preyPred[1] = 19.92
After 2 generations: Hares = 219.31183648512007 and Lynx = 19.726535847029762

      

+3


source to share


2 answers


Arrays in Java are not passed as a copy. They are passed as a reference to an array, which is then shared between the caller and the method.

So, if you update an array from a method, it is an in-place update and the changes will be visible to whoever else has a link to that array.



If you want to avoid this, make a "defensive copy" (using Arrays.copyOf) and change just that.

+4


source


You pass an array reference to preyPredDefault

your method preyPredLV

, and your method updates that array with the reference you pass. Thus, your method changes the contents of this array.

If you don't want preyPredLV

this array to be updated, you can pass a copy of the array to it.



double[] result = preyPredLV(Arrays.copyOf(preyPredDefault,preyPredDefault.length), a, b, n);

      

+2


source







All Articles