简体   繁体   中英

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. 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:

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:

  1. you're using Random wrong. 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; it's a property of sparse , not int . You could have done 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. 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. Similarly, your inner j for loop needs to stop iterating when it reaches the length of the matrix[i] array.

If you want to assign a value to one particular spot in your 2D array, then you need one assignment, not two; on the left-hand side of the = you'll need to use both i and 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.

You aren't even calling the matrixGen method.

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. 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. 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;
  2. Your loop conditions were wrong. The indexes are incremented until they reach the respective length ( i for lines and j for columns);
  3. Access the matrix cell using two indexes (line and column);
  4. randomGen is a method from class sparse , and not from arrays.

Edit: as noted by Dave, you shouldn't also be creating a new Random instance every time. Either store the Random in a variable, as you did with the matrix, or create it before entering the loops, in this same method.

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