java - Generating random non-repeating numbers 2d array - all numbers same -


This question is for Java, I'm trying to make the size N block [] [], where number 0 - 9 is randomly placed. I have created an array and then repeating it, it turned round to prevent numbers. When I repeat on the array and allocate the two-dimensional array to the numbers, then I continue to find that all the numbers in the two-dimensional array are same. The block stays on the following:

222

222

222

I highly appreciate your help! Public Class Board {int n = 3; Fixed int [] [] copy; // Build a board with an N-by-N array of blocks / (where block [i] [j] = block in line I, column j) Public board (int [] [] block) {blocks = new int [N] [n]; // creates an array of size N / # unique list & lt; Integer & gt; RandomList = New Arrielist & lt; & Gt; (); (Int i = 0; i

  int counter = 0; Collections.shuffle (randomList); For (int i = 0; i & lt; blocks.length; i ++) {for (int j = 0; j & lt; block [i]. Length; j ++) {block [i] [j] ] = randomList.get (counter); Counter ++; }}   

You must assign a multi-dimensional array by creating a loop through each element of your original array

Also replace: < For pre> (int i = 0; i & lt; 10; i ++) {randomList.add (i); }

You say that you want Loop given above 0- 9 will give you 0-9. You will have Loop only 0-2.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -