java - can't find source of inadvertent loop -


i started refactoring program working on , hit major road block... have 1 class acts nucleus, 6 other smaller (but still important) classes working run program... took 1 method [called 'populate()'] out nucleus class , made entirely new class [called 'populationgenerator'], when try create object of newly created class anywhere in nucleus class stuck in never ending loop of new class

i've never had issue when trying create objects before... here's nucleus class before refactoring:

    public class simulator {     // constants representing configuration information simulation.     // default width grid.     private static final int default_width = 120;     // default depth of grid.     private static final int default_depth = 80;     // probability fox created in given grid position.     private static final double fox_creation_probability = 0.02;     // probability rabbit created in given grid position.     private static final double rabbit_creation_probability = 0.08;          // list of animals in field.     private list<animal> animals;     // current state of field.     private field field;     // current step of simulation.     private int step;     // graphical view of simulation.     private simulatorview view;      /**      * construct simulation field default size.      */     public simulator()     {         this(default_depth, default_width);     }      /**      * create simulation field given size.      * @param depth depth of field. must greater zero.      * @param width width of field. must greater zero.      */     public simulator(int depth, int width)     {         if(width <= 0 || depth <= 0) {             system.out.println("the dimensions must greater zero.");             system.out.println("using default values.");             depth = default_depth;             width = default_width;         }          animals = new arraylist<animal>();         field = new field(depth, width);          // create view of state of each location in field.         view = new simulatorview(depth, width);         view.setcolor(rabbit.class, color.orange);         view.setcolor(fox.class, color.blue);          // setup valid starting point.         reset();     }      /**      * run simulation current state reasonably long period,      * (4000 steps).      */     public void runlongsimulation()     {         simulate(4000);     }      /**      * run simulation current state given number of steps.      * stop before given number of steps if ceases viable.      * @param numsteps number of steps run for.      */     public void simulate(int numsteps)     {         for(int step = 1; step <= numsteps && view.isviable(field); step++) {             simulateonestep();         }     }      /**      * run simulation current state single step.      * iterate on whole field updating state of each      * fox , rabbit.      */     public void simulateonestep()     {         step++;          // provide space newborn animals.         list<animal> newanimals = new arraylist<animal>();                 // let rabbits act.         for(iterator<animal> = animals.iterator(); it.hasnext(); ) {             animal animal = it.next();             animal.act(newanimals);             if(! animal.isalive()) {                 it.remove();             }         }          // add newly born foxes , rabbits main lists.         animals.addall(newanimals);          view.showstatus(step, field);     }      /**      * reset simulation starting position.      */     public void reset()     {         step = 0;         animals.clear();         populate();          // show starting state in view.         view.showstatus(step, field);     }      /**      * randomly populate field foxes , rabbits.      */     private void populate()     {         random rand = randomizer.getrandom();         field.clear();         for(int row = 0; row < field.getdepth(); row++) {             for(int col = 0; col < field.getwidth(); col++) {                 if(rand.nextdouble() <= fox_creation_probability) {                     location location = new location(row, col);                     fox fox = new fox(true, field, location);                     animals.add(fox);                 }                 else if(rand.nextdouble() <= rabbit_creation_probability) {                     location location = new location(row, col);                     rabbit rabbit = new rabbit(true, field, location);                     animals.add(rabbit);                 }                 // else leave location empty.             }         }     } } 

edit:

here's same class after refactoring ...

import java.util.random; import java.util.list; import java.util.arraylist; import java.util.iterator; import java.awt.color;  /**  * simple predator-prey simulator, based on rectangular field  * containing rabbits , foxes.   *   * update 10.40:  * *almost* decoupled concrete animal classes.  *   * @twisted_crystals  */ public class simulator {     // constants representing configuration information simulation.     // default width grid.     private static final int default_width = 120;     // default depth of grid.     private static final int default_depth = 80;        // current state of field.     private field field;     // current step of simulation.     private int step;     // graphical view of simulation.     private simulatorview view;     //population generator class... coupled fox , rabbit classes     private populationgenerator popgenerator;     // lists of animals in field. separate lists kept ease of iteration.     private list<animal> animals;      /**      * construct simulation field default size.      */     public simulator()     {               this(default_depth, default_width);      }      /**      * create simulation field given size.      * @param depth depth of field. must greater zero.      * @param width width of field. must greater zero.      */     public simulator(int depth, int width)     {          if(width <= 0 || depth <= 0) {             system.out.println("the dimensions must greater zero.");             system.out.println("using default values.");             depth = default_depth;             width = default_width;         }          animals = new arraylist<animal>();         field = new field(depth, width);          // create view of state of each location in field.         //          //         view.setcolor(rabbit.class, color.orange); // pg         //         view.setcolor(fox.class, color.blue); // pg          // setup valid starting point.         reset();     }      /**      * run simulation current state reasonably long period,      * (4000 steps).      */     public void runlongsimulation()     {         simulate(4000);      }      /**      * run simulation current state given number of steps.      * stop before given number of steps if ceases viable.      * @param numsteps number of steps run for.      */     public void simulate(int numsteps)     {         for(int step = 1; step <= numsteps && view.isviable(field); step++) {             simulateonestep();         }     }      /**      * run simulation current state single step.      * iterate on whole field updating state of each      * fox , rabbit.      */     public void simulateonestep()     {         step++;          // provide space animals.         list<animal> newanimals = new arraylist<animal>();                 // let animals act.         for(iterator<animal> = animals.iterator(); it.hasnext(); ) {             animal animal = it.next();             animal.act(newanimals);             if(! animal.isalive()) {                 it.remove();             }         }          animals.addall(newanimals);      }      /**      * reset simulation starting position.      */     public void reset()     {         populationgenerator popgenerator = new populationgenerator();          step = 0;          animals.clear();          popgenerator.populate();          // show starting state in view.         view.showstatus(step, field);     }      public int getstep()     {         return step;     }  } 

... , new class

import java.util.arraylist; import java.util.random; import java.util.list; import java.awt.color;  public class populationgenerator {     // default width grid.     private static final int default_width = 120;     // default depth of grid.     private static final int default_depth = 80;      // probability fox created in given grid position.     private static final double fox_creation_probability = 0.02;     // probability rabbit created in given grid position.     private static final double rabbit_creation_probability = 0.08;      // lists of animals in field. separate lists kept ease of iteration.     private list<animal> animals;     // current state of field.     private field field;     // graphical view of simulation.     private simulatorview view;      /**      * constructor      */     public populationgenerator()     {         animals = new arraylist<animal>();          field = new field(default_depth, default_width);      }      /**      * randomly populate field foxes , rabbits.      */     public void populate()     {         // create view of state of each location in field.         view = new simulatorview(default_depth, default_width);          view.setcolor(rabbit.class, color.orange); // pg         view.setcolor(fox.class, color.blue); // pg          simulator simulator = new simulator();         random rand = randomizer.getrandom();         field.clear();         for(int row = 0; row < field.getdepth(); row++) {             for(int col = 0; col < field.getwidth(); col++) {                 if(rand.nextdouble() <= fox_creation_probability) {                     location location = new location(row, col);                     fox fox = new fox(true, field, location);                     animals.add(fox);                 }                 else if(rand.nextdouble() <= rabbit_creation_probability) {                     location location = new location(row, col);                     rabbit rabbit = new rabbit(true, field, location);                     animals.add(rabbit);                 }                 // else leave location empty.             }         }         view.showstatus(simulator.getstep(), field);     } } 

here's field class populationgenerator calls... havent changed class in way

import java.util.collections; import java.util.iterator; import java.util.linkedlist; import java.util.list; import java.util.random;  /**  * represent rectangular grid of field positions.  * each position able store single animal.  *   * @twisted_crystals  */ public class field {     // random number generator providing random locations.     private static final random rand = randomizer.getrandom();      // depth , width of field.     private int depth, width;     // storage animals.     private object[][] field;      /**      * represent field of given dimensions.      * @param depth depth of field.      * @param width width of field.      */     public field(int depth, int width)     {         this.depth = depth;         this.width = width;         field = new object[depth][width];     }      /**      * empty field.      */     public void clear()     {         for(int row = 0; row < depth; row++) {             for(int col = 0; col < width; col++) {                 field[row][col] = null;             }         }     }      /**      * clear given location.      * @param location location clear.      */     public void clear(location location)     {         field[location.getrow()][location.getcol()] = null;     }      /**      * place animal @ given location.      * if there animal @ location      * lost.      * @param animal animal placed.      * @param row row coordinate of location.      * @param col column coordinate of location.      */     public void place(object animal, int row, int col)     {         place(animal, new location(row, col));     }      /**      * place animal @ given location.      * if there animal @ location      * lost.      * @param animal animal placed.      * @param location place animal.      */     public void place(object animal, location location)     {         field[location.getrow()][location.getcol()] = animal;     }      /**      * return animal @ given location, if any.      * @param location in field.      * @return animal @ given location, or null if there none.      */     public object getobjectat(location location)     {         return getobjectat(location.getrow(), location.getcol());     }      /**      * return animal @ given location, if any.      * @param row desired row.      * @param col desired column.      * @return animal @ given location, or null if there none.      */     public object getobjectat(int row, int col)     {         return field[row][col];     }      /**      * generate random location adjacent      * given location, or same location.      * returned location within valid bounds      * of field.      * @param location location generate adjacency.      * @return valid location within grid area.      */     public location randomadjacentlocation(location location)     {         list<location> adjacent = adjacentlocations(location);         return adjacent.get(0);     }      /**      * shuffled list of free adjacent locations.      * @param location locations adjacent this.      * @return list of free adjacent locations.      */     public list<location> getfreeadjacentlocations(location location)     {         list<location> free = new linkedlist<location>();         list<location> adjacent = adjacentlocations(location);         for(location next : adjacent) {             if(getobjectat(next) == null) {                 free.add(next);             }         }         return free;     }      /**      * try find free location adjacent      * given location. if there none, return null.      * returned location within valid bounds      * of field.      * @param location location generate adjacency.      * @return valid location within grid area.      */     public location freeadjacentlocation(location location)     {         // available free ones.         list<location> free = getfreeadjacentlocations(location);         if(free.size() > 0) {             return free.get(0);         }         else {             return null;         }     }      /**      * return shuffled list of locations adjacent given one.      * list not include location itself.      * locations lie within grid.      * @param location location generate adjacencies.      * @return list of locations adjacent given.      */     public list<location> adjacentlocations(location location)     {         assert location != null : "null location passed adjacentlocations";         // list of locations returned.         list<location> locations = new linkedlist<location>();         if(location != null) {             int row = location.getrow();             int col = location.getcol();             for(int roffset = -1; roffset <= 1; roffset++) {                 int nextrow = row + roffset;                 if(nextrow >= 0 && nextrow < depth) {                     for(int coffset = -1; coffset <= 1; coffset++) {                         int nextcol = col + coffset;                         // exclude invalid locations , original location.                         if(nextcol >= 0 && nextcol < width && (roffset != 0 || coffset != 0)) {                             locations.add(new location(nextrow, nextcol));                         }                     }                 }             }              // shuffle list. several other methods rely on list             // being in random order.             collections.shuffle(locations, rand);         }         return locations;     }      /**      * return depth of field.      * @return depth of field.      */     public int getdepth()     {         return depth;     }      /**      * return width of field.      * @return width of field.      */     public int getwidth()     {         return width;     } } 

your problem not in field class below it. simulator constructor calls reset() creates new populationgenerator object, calls populate() on object. populate() method calls simulator simulator = new simulator(); creates new simulator object continues cycle. solution: don't create new simulator object in populationgenerator, instead pass existing simulator populationgenerator through constructor or through setsimulator(...) method.

e.g.,

class populationgenerator {   // ... etc...   private simulator simulator;  // null begin    // pass simulator instance constructor.    // need same simulatorview   public populationgenerator(simulator simulator, int depth, int width) {     this.simulator = simulator; // set instance      // ... more code etc...    }    public void populate() {     // don't re-create simulator here rather use instance passed in   }  } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -