[英]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)件事:
Random
wrong. Random
错误。 You should create it once, then get lots of numbers from it. sparse
, not int
. sparse
的属性,而不是int
。 You could have done matrix[i][j] = randomGen()
. matrix[i][j] = randomGen()
。 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
. 上的左手侧
=
你需要同时使用i
和j
。
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();
}
}
}
void
, as you're modifying your matrix, and not creating a new one; void
,因为您要修改矩阵,而不要创建一个新矩阵; i
for lines and j
for columns); i
代表行, j
代表列); 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.