Java Iterating Through 2D-Array -


i have 2d array in java so:

int 2d_arr[5][5]; 

take, example, board looks this:

1 1 1 1 1 0 1 1 1 1 1 1 2 1 0 0 1 1 1 1 0 0 1 1 1 

starting 2 in 3rd row, want able move in every direction (up, down, left, right, , diagonals). in code, how traverse array in every direction till find 0?

my theoretical idea iterate in every direction sequentially. example, start going up, check values on line above 2

1 1 2 

since didn't find zeros, check top right diagonal

   1   1  2 

still no 0s, go onto right. find first 0, break.

attempt: know how bunch of if , loops, looking way edit code simpler , easier read version

but i'm new java don't know best way go this. ideas?

a twod iterator start. expect rest without effort.

finding first 0 in scenario involve iterating through eachdirection , iterating across board in direction until iteration ends or find zero.

doing spiral search involve starting iterator in each direction , doing step , check on each 1 in turn until 1 returns point 0 can found.

public class twoditeratortest {      // ubiquitous point class     static class point {         final int x;         final int y;          public point(int x, int y) {             this.x = x;             this.y = y;         }          @override         public string tostring() {             return "{" + x + "," + y + "}";         }     }      // possible directions.     enum direction {         north(0, 1),         northeast(1, 1),         east(1, 0),         southeast(1, -1),         south(0, -1),         southwest(-1, -1),         west(-1, 0),         northwest(-1, 1);         private final int dx;         private final int dy;          direction(int dx, int dy) {             this.dx = dx;             this.dy = dy;         }          // step way         public point step(point p) {             return new point(p.x + dx, p.y + dy);         }     }      static class twoditerator implements iterable<point> {         //         point i;         // direction move in.         private final direction step;         // limits.         private final point min;         private final point max;         // next position go to.         point next = null;          // normal constructor.         public twoditerator(point start, direction step, point min, point max) {             = next = start;             this.step = step;             this.min = min;             this.max = max;         }          // simplified constructors         public twoditerator(int x, int y, direction step, point min, point max) {             this(new point(x, y), step, min, max);         }          public twoditerator(int x, int y, direction step, int minx, int miny, int maxx, int maxy) {             this(new point(x, y), step, new point(minx, miny), new point(maxx, maxy));         }          // iterator.         @override         public iterator<point> iterator() {             return new iterator<point>() {                 // hasnext calculates next if necessary , checks against stabliched limits.                 @override                 public boolean hasnext() {                     if (next == null) {                         // step one.                         next = step.step(i);                         // stop @ limits.                         if (next.x < min.x                                 || next.x > max.x                                 || next.y < min.y || next.y > max.y) {                             next = null;                         }                     }                     return next != null;                 }                  @override                 public point next() {                     if (hasnext()) {                         // make our move.                         = next;                         next = null;                         return i;                     }                     return null;                 }                  @override                 public void remove() {                     throw new unsupportedoperationexception("not supported.");                 }             };         }     }      public void test() {         // test directions.         (direction d : direction.values()) {             system.out.print(d + " - ");             (point p : new twoditerator(0, 0, d, -5, -5, 5, 5)) {                 system.out.print(p + ",");             }             system.out.println();         }     }      public static void main(string[] args) throws interruptedexception {         new twoditeratortest().test();     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -