mardi 14 avril 2015

How to make a random 2D jagged array of varying length?

I have to create a 2D jagged array with a random number of rows (5-10) with each row having a random length (5-10). I filled the jagged array with random numbers. It should look something like


2 4 1 5 3 8 6 3


2 5 8 9 7 4 3 5 6


6 7 9 3 5


2 6 7 8 4 5 3 6 7


1 4 2 2 1


This is my current createArray method



public static int [][] createArray(){
int row = (int)(Math.random()*5)+5;
int column = (int)(Math.random()*5)+5;

int[][]array = new int[row][];

for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
//Fill the matrix with random numbers
array[i][j] = (int)(Math.random()*10);
}}

return array;
}//End createArray method


However, this just randomizes the rows and columns and doesn't create a jagged array. Can anyone help lead me in the right direction? Thanks a lot!





Aucun commentaire:

Enregistrer un commentaire