【问题标题】:Java Iterating Through 2D-ArrayJava遍历二维数组
【发布时间】:2013-03-14 07:46:45
【问题描述】:

我在 Java 中有一个二维数组,如下所示:

int 2d_arr[5][5];

以如下图为例:

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

从第 3 行的 2 开始,我希望能够在各个方向(上、下、左、右和对角线)移动。在代码中,如何在每个方向遍历数组直到找到 0?

我的理论想法是在每个方向上依次迭代。例如,从上升开始,所以我会检查上面 2 行的所有值

1
1
2

由于我没有找到任何零,请检查右上角的对角线

   1
  1
 2

仍然没有 0,所以向右走。一旦我找到我的第一个 0,就打破。

尝试: 我实际上知道如何使用一堆 if 和 for 循环来做到这一点,但我正在寻找一种方法来将该代码编辑为更简单、更易于阅读的版本

但我是 java 新手,所以我不知道解决这个问题的最佳方法。有什么想法吗?

【问题讨论】:

  • 你有尝试过什么吗? (提出更好问题的有用链接:How to AskFAQ
  • 你为什么从2开始?你需要找到最接近 2 的 0 吗?
  • 是的,这两个是重要的部分。如果有的话,我需要在它的 8 个方向中找到任何零。
  • 所以你需要知道找到的第一个零的位置,需要知道你可以进入多少个方向才能找到 0,或者需要关于是否有零的真/假答案8 个方向中的任何一个?
  • 只需要一个真/假,在任何 8 个方向上都有任何 0。甚至不需要遍历所有 8 个方向,只需为找到的第一个零返回 true。

标签: java arrays multidimensional-array iteration


【解决方案1】:

TwoD 迭代器显然是一个好的开始。我希望您可以轻松完成其余的工作。

在您的场景中找到第一个零将涉及遍历 eachDirection 并在该方向上全面迭代,直到迭代结束或您找到零。

进行螺旋搜索将涉及在每个方向上启动一个迭代器并执行一个步骤并依次检查每个方向,直到一个返回可以找到零的点。

public class TwoDIteratorTest {

    // An 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 + "}";
        }
    }

    // All 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 that way
        public Point step(Point p) {
            return new Point(p.x + dx, p.y + dy);
        }
    }

    static class TwoDIterator implements Iterable<Point> {
        // Where we are now
        Point i;
        // Direction to move in.
        private final Direction step;
        // Limits.
        private final Point min;
        private final Point max;
        // Next position to go to.
        Point next = null;

        // Normal constructor.
        public TwoDIterator(Point start, Direction step, Point min, Point max) {
            i = next = start;
            this.step = step;
            this.min = min;
            this.max = max;
        }

        // Some 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));
        }

        // The iterator.
        @Override
        public Iterator<Point> iterator() {
            return new Iterator<Point>() {
                // hasNext calculates next if necessary and checks it against the stabliched limits.
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        // Step one.
                        next = step.step(i);
                        // Stop at 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.
                        i = next;
                        next = null;
                        return i;
                    }
                    return null;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Not supported.");
                }
            };
        }
    }

    public void test() {
        // Test all directions.
        for (Direction d : Direction.values()) {
            System.out.print(d + " - ");
            for (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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2014-12-22
    • 2011-07-18
    相关资源
    最近更新 更多