简体   繁体   English

在Java中将随机数传递给2D数组

[英]passing random numbers to a 2d array in java

Why am i getting erros in my code - I created two methods, randomGen to generate a random number and matrixGen to create the matrix with random numbers. 为什么在我的代码中出现错误-我创建了两个方法,randomGen生成随机数,而matrixGen创建带有随机数的矩阵。 I am getting incompatible types error. 我收到不兼容的类型错误。 If someone can please point me in the right direction to what I could be doing wrong.. Im still in learning phase.. Heres my code: 如果有人可以将我的正确方向指向我可能会做错的事情。.我仍处于学习阶段..这是我的代码:

import java.util.Random;

public class sparse{
    static int matrix [][] = new int[6][6];

    public static int randomGen(){
        int rA;
        Random r = new Random();
        rA = r.nextInt(100);
        return rA;
    }

    public static int[][] matrixGen(){
        for(int i=0; matrix[i].length < i; i++){
            for(int j=0; matrix[j].length <j; j++){
                matrix[i] = matrix[i].randomGen();
                matrix[j] = matrix[j].randomGen();
            }
        }
        return matrix[i][j];
    }

    public static void main(String args[]){
        new sparse();
    }
}

Get rid of randomGen, use this: 摆脱randomGen,使用它:

public static int[][] matrixGen(){
    Random r = new Random( );
    for(int i=0; i < matrix.length; i++){
        for(int j=0; j < matrix[i].length; j++){
            matrix[i][j] = r.nextInt( 100 );
        }
    }
    return matrix;
}

3 (update: 4) things: 3(更新:4)件事:

  1. you're using Random wrong. 您使用Random错误。 You should create it once, then get lots of numbers from it. 您应该创建一次,然后从中获取很多数字。
  2. you're trying to call randomGen on an int, which makes no sense; 您试图在int上调用randomGen,这没有任何意义; it's a property of sparse , not int . 这是sparse的属性,而不是int You could have done matrix[i][j] = randomGen() . 您可以完成matrix[i][j] = randomGen()
  3. you're doing something very odd indeed to access array elements. 您确实在做一些很奇怪的事情来访问数组元素。 Hopefully this code will clear it up for you a bit. 希望这段代码可以为您清除一点。
  4. your loops are odd too. 您的循环也很奇怪。 I've fixed them in this snippet. 我已经在此代码段中修复了它们。

This: 这个:

    matrix[i] = matrix[i].randomGen();
    matrix[j] = matrix[j].randomGen();

needs to be this: 需要这样的:

    matrix[i][j] = randomGen();

Your matrixGen method is confused. 您的matrixGen方法感到困惑。 Your outer i for loop needs to stop iterating when it reaches the length of the matrix array, not the length of the i th inner array. 当外部i for循环达到matrix数组的长度而不是第i内部数组的长度时,需要停止迭代。 Similarly, your inner j for loop needs to stop iterating when it reaches the length of the matrix[i] array. 类似地,内部j for循环在达到matrix[i]数组的长度时需要停止迭代。

If you want to assign a value to one particular spot in your 2D array, then you need one assignment, not two; 如果要为2D数组中的一个特定点分配值,则需要一个分配,而不是两个; on the left-hand side of the = you'll need to use both i and j . 上的左手侧=你需要同时使用ij

You don't need to put matrix[i] or matrix[j] before your call to randomGen , because you defined it in the same class. 调用randomGen之前,无需将matrix[i]matrix[j]放在同一个类中。

You aren't even calling the matrixGen method. 您甚至都没有调用matrixGen方法。

First of all, in the provided piece of code, your main does nothing at all, since you create an object which you don't use. 首先,在提供的代码段中,您的main根本不执行任何操作,因为您创建了不使用的对象。 I assume it is going to be continued in the future. 我认为将来还会继续。

As to the errors you're getting, I'll also have to make assumptions, as no additional information is provided. 至于您遇到的错误,我也必须做一些假设,因为没有提供其他信息。

The errors you're encountering are probably in your matrixGen method, which, I assume, is supposed to populate the static matrix. 您遇到的错误可能在您的matrixGen方法中,我认为该方法应该填充静态矩阵。 It should be like this: 应该是这样的:

public static void matrixGen(){
    for(int i=0; i < matrix.length; i++){
        for(int j=0; j < matrix[i].length; j++){
            matrix[i][j] = randomGen();
        }
    }
}
  1. void , as you're modifying your matrix, and not creating a new one; void ,因为您要修改矩阵,而不要创建一个新矩阵;
  2. Your loop conditions were wrong. 您的循环条件错误。 The indexes are incremented until they reach the respective length ( i for lines and j for columns); 索引递增直到它们达到各自的长度( i代表行, j代表列);
  3. Access the matrix cell using two indexes (line and column); 使用两个索引(行和列)访问矩阵单元
  4. randomGen is a method from class sparse , and not from arrays. randomGen是来自class sparse的方法,而不是来自数组的方法。

Edit: as noted by Dave, you shouldn't also be creating a new Random instance every time. 编辑:正如Dave所说,您也不应该每次都创建一个新的Random实例。 Either store the Random in a variable, as you did with the matrix, or create it before entering the loops, in this same method. 可以像处理矩阵一样将Random存储在变量中,也可以使用相同的方法在进入循环之前创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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