java - Setting instance variables in a loop -


i trying make minesweeper in java , created box class, instance variable ismine determines whether or not box mine. use setter set variable, although problem exists when use constructor instead. in main method when create , fill arraylist containing arraylists of boxes, use random number generator set ismine variables. inside same loop use fill arraylists.

    arraylist<arraylist<box>> boxlist = new arraylist<arraylist<box>>();     for(int i=0; i<10; i++){         boxlist.add(new arraylist<box>());     }     for(int i=0; i<boxlist.size(); i++){         for(int j=0; j<10; j++){             double rand = math.random();             boxlist.get(i).add(new box());             boxlist.get(i).get(j).drawbox(i,j);             rand = math.random();             if(rand>.2){                 boxlist.get(i).get(j).setmine(false);             }             else{                 boxlist.get(i).get(j).setmine(true);             }         }     } 

however seems every box's ismine set whatever last box's ismine set to. if boxlist.get(9).get(9).getmine() false, false.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -