Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使方法中的数组在每次调用该方法时不发生更改_Java - Fatal编程技术网

Java 如何使方法中的数组在每次调用该方法时不发生更改

Java 如何使方法中的数组在每次调用该方法时不发生更改,java,Java,我制作了一个使用random类生成数字的方法。这些数字用于填充数组。此方法返回填充的数组。因此,我还制作了一些其他数组,它们与返回的数组相等。但是,这些数组都是相同的,即使它们应该是随机的 我试着打印每个数组调用随机数的方式,就在它调用的位置下面。这给了我不同的数组。但是如果我把指纹移到所有调用下面,那么数组都是一样的。另外,我认为这是一个数组问题,因为我只有数组的问题 public static int[] numberGenerator(int[] array) {

我制作了一个使用random类生成数字的方法。这些数字用于填充数组。此方法返回填充的数组。因此,我还制作了一些其他数组,它们与返回的数组相等。但是,这些数组都是相同的,即使它们应该是随机的

我试着打印每个数组调用随机数的方式,就在它调用的位置下面。这给了我不同的数组。但是如果我把指纹移到所有调用下面,那么数组都是一样的。另外,我认为这是一个数组问题,因为我只有数组的问题

    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]);          
    }
}
公共静态int[]numberGenerator(int[]array){
Random rand=新的Random();
int min=0;
int max=0;
int x=0;
对于(int j=0;j

我正在寻找不同的数组。

您将相同的数组对象传递给对
numberGenerator
的两个调用,因此它们当然返回相同的数组(因为
numberGenerator
不创建新数组,它只填充传递给它的数组)

您可以在每次调用中传递不同的数组:

int[] array1 = numberGenerator(new int[3]);  
int[] array2 = numberGenerator(new int[3]);
或者,将数组长度传递给
numberGenerator
,让该方法创建数组:

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

    ... fill the array ...

    return array;
}
并将其用于:

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

您正在修改传递给方法的数组。假设您不使用数组的现有内容,最好只传入所需的长度,然后在方法中创建一个新数组:

public static int[] numberGenerator(int length) {
    int[] array = new int[length];
    // Rest of method as before
}
然后将
main
方法更改为:

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

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

您必须了解在Java中按引用传递和按值传递是如何工作的。数组被视为对象,它们通过引用传递到方法中。这意味着,在您的代码中只有一个物理数组
array
,它正在被传递,所有的修改都在它上面进行


您可以按照其他答案建议的任何设计来解决您的问题

不要使用传递的参数来存储生成的随机数。您应该在方法中创建一个新数组来存储随机数。这是因为该方法的数组类型的参数是引用。返回的数组实际上是对该数组的引用。此数组的值取决于上次修改。以上array1的值将成为最后一次修改。这就是为什么数组1与数组2相同,因为它们都指向一个数组

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 ;
}
公共静态int[]numberGenerator(int[]array){
Random rand=新的Random();
int min=0;
int max=0;
int x=0;
int[]newArray=newint[array.length];
对于(int j=0;j
尝试在方法调用之间添加print语句,看看这是否能告诉您更多关于问题所在的信息。如果您正在使用任何IDE(如Eclipse),请使用调试器。这将为您(以及stackoverflow用户)节省大量时间。祝您编码愉快,欢迎来到stackoverflow的世界。或许可以看看: