简体   繁体   中英

How to make an array from a method not change each time the method is called

I made a method that uses the random class to generate a number. These numbers are used to fill in an array. This method returns the filled in array. So, I have a few other arrays that I made which are equal to the returned array. However, these arrays are all the same even though they should be random.

I tried printing how each array that calls the random, right under where it calls. This gave me different arrays. But if I move my prints to under all the calls then the arrays are all the same. Also I think this is an array issue because I only have this problem with arrays.

    public static int[] numberGenerator(int[] array) {
        Random rand = new Random();
        int min = 0;
        int max = 0;
        int x = 0;

        for (int j = 0; j < array.length; j++) {        

            if (j == 0) {
                min = 1;
                max = 15;
            }
            if (j == 1) {
                min = 16;
                max = 30;
            }
            if (j == 2) {
                min = 31;
                max = 45;
            }
            x = rand.nextInt((max - min) + 1) + min;
            array[j] = x;                               
        }
        return array;
    }

    public static void main(String[] args) {
        int[] array = {' ', ' ', ' '};
        int[] array1 = numberGenerator(array);  
        int[] array2 = numberGenerator(array);

        System.out.println(array1[1]);
        System.out.println(array2[1]);          
    }
}

Distinct arrays are what I'm looking for.

You're modifying the array that you pass into the method. Given that you don't use the existing content of the array anyway, it would be better to just pass in the length that you want, and create a new array in the method:

public static int[] numberGenerator(int length) {
    int[] array = new int[length];
    // Rest of method as before
}

Then change your main method to:

public static void main(String[] args) {
    int[] array1 = numberGenerator(3);  
    int[] array2 = numberGenerator(3);

    System.out.println(array1[1]);
    System.out.println(array2[1]);          
}

You have to understand how pass by reference and pass by value work in Java. Arrays are considered as objects, and they get passed into methods by reference. This means, in your code there is only one physical array array and that is getting passed along, and all the modifications are being done on it.

You can follow any of the designs suggested by other answers to solve your problem!

You are passing the same array object to the two calls to numberGenerator , so of course they return the same array (since numberGenerator doesn't create a new array, it just fills the array you pass to it).

You can pass a different array in each call:

int[] array1 = numberGenerator(new int[3]);  
int[] array2 = numberGenerator(new int[3]);

Or, as an alternative, pass an array length to numberGenerator , and let that method create the array:

public static int[] numberGenerator(int length) {
    int[] array = new int[length];

    ... fill the array ...

    return array;
}

and use it with:

int[] array1 = numberGenerator(3);  
int[] array2 = numberGenerator(5);

Don't use the passed parameters to store the generated random number. You should create a new array in the method to store the random number. This is because the argument to the array type of the method is a reference. The array you return is actually a reference to this array. The value of this array depends on your last modification. The value of array1 above will become the last modification. This is why array1 is the same as array 2 because they all point to an array.

public static int[] numberGenerator(int[] array) {
    Random rand = new Random();
    int min = 0;
    int max = 0;
    int x = 0;
    int[] newArray = new int[array.length];
    for (int j = 0; j < array.length; j++) {

        if (j == 0) {
            min = 1;
            max = 15;
        }
        if (j == 1) {
            min = 16;
            max = 30;
        }
        if (j == 2) {
            min = 31;
            max = 45;
        }
        x = rand.nextInt((max - min) + 1) + min;
        newArray [j] = x;
    }
    return newArray ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM