java - How to fill in [x]% of a 2d array with the values of another array -


i have 2 two-dimensional arrays, 1 filled , 1 filled, both of same size (n x m). need fill in [some random]% of second array exact same values first array. let's say

int[][] arr1 = new int[][] {{0, 1, 0, 1},                             {1, 0, 1, 0},                             {0, 0, 1, 1},                             {1, 1, 0, 0}}; 

i want arr2 filled 25% of arr1's values in same spot other spots remaining null..

int[][] arr2 = new int[4][4]; // arr2 = {{0,1,0,1}, //                              { , , , }, //                              { , , , }, //                              { , , , }} 

eventually, program call on user fill in missing values, can do, reason part seems not want work me. school assignment , teacher isn't being helpful answering questions.

here's code: noting (variable names in french..)

  1. nbcases = total number of elements in 2d array
  2. nbaremplir = number of elements in 2nd array have values assigned them (based on nbcases * nbpourc nbpourc percentage of 2nd array assigned values)
  3. grille[][] = original array
  4. gui = second "array" (it's 2d array of jbuttons teacher wrote..syntax add value : gui.setvaleur(i, j, value) )

    int nbcases = grille.length * grille[0].length; int nbaremplir = (int)(nbcases * nbpourc);  do{     for(int = 0; < grille.length; i++) {         for(int j = 0; j < grille[i].length; j++) {             if(rand.nextint(2) == 1 && nbaremplir > 0) {                 gui.setvaleur(i, j, "" + grille[i][j]);                 nbaremplir--;             }         }     }        } while(nbaremplir > 0); 

thanks help! :)

since randomly choose set values, might run through 2d array multiple times (due while-loop). therefore gonna set value @ same (i,j) index multiple times, still decrementing nbaremplir.

now, don't know type gui is, change if-statement like

if(gui[i][j] != null && rand.nextint(2) == 1 && nbaremplir > 0)... 

this ensures not gonna put in same value/object twice.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -