如何调用在另一堂课中启动的2D数组?

发布于 2025-01-25 18:12:34 字数 851 浏览 3 评论 0原文

我简化了我的问题的最小示例:迷宫类用方法generateMaze()mazearray的内容>在此示例中是无关紧要的)。 Walker调用该方法的主线程,从而从Maze> Maze类创建此mazearray

我不明白如何在walker.walk中调用此数组?恐怕我有一个知识差距。

每个提示都非常感谢,非常感谢。

public final class Maze {

public static boolean[][] generateMaze(int width, int height) {

    boolean[][] mazeArray = new boolean[width][height];

    for( int x = 0; x < width; x++ ) {
        mazeArray[x][0] = true;
    }
    for( int y = 0; y < height; y++ ) {
        mazeArray[0][y] = true;
    }
    return mazeArray;
}

}

public class Walker {

public static void main(String[] args) {
    Maze mazeObj  = new Maze();
    boolean[][] maze = Maze.generateMaze(2,2);
}

public void walk(Maze maze) {
   // Traverse Array
}

}

I made a minimal reduced example of my problem: the Maze class creates a 2D boolean array with the method generateMaze() (the content of mazeArray is irrelevant in this example). The main thread from Walker calls that method and thereby creates this mazeArray from the Maze class.

I do not understand how I can call this array in Walker.walk? I'm afraid I have a knowledge gap.

Every hint is appreciated, thank you very much.

public final class Maze {

public static boolean[][] generateMaze(int width, int height) {

    boolean[][] mazeArray = new boolean[width][height];

    for( int x = 0; x < width; x++ ) {
        mazeArray[x][0] = true;
    }
    for( int y = 0; y < height; y++ ) {
        mazeArray[0][y] = true;
    }
    return mazeArray;
}

}

public class Walker {

public static void main(String[] args) {
    Maze mazeObj  = new Maze();
    boolean[][] maze = Maze.generateMaze(2,2);
}

public void walk(Maze maze) {
   // Traverse Array
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2025-02-01 18:12:34

说明

这里有几个基本的OOP 错误。

首先,为什么当您的Maze类创建一个实例时,当您的generateMaze类是static,然后返回迷宫作为<代码>布尔值[] [] 而不是迷宫。您可能打算将数组作为类的字段,而不是直接通过迷宫实例访问数组。

接下来,walk方法是非静态的,并且是Walker实例的一部分。因此,您需要创建该类的实例并在该实例上调用该方法。


迷宫生成

您可能打算这样做:

public final class Maze {
  // Arrays as field of maze instances
  private boolean[][] mazeArray;

  // return maze instance instead of array
  public static Maze generateMaze(int width, int height) {
    // create maze instance
    Maze maze = new Maze();
    // manipulate array of that maze instance
    maze.mazeArray = new boolean[width][height];

    for (int x = 0; x < width; x++) {
        maze.mazeArray[x][0] = true;
    }
    for (int y = 0; y < height; y++) {
        maze.mazeArray[0][y] = true;
    }

    // return the maze, not its array
    return maze;
  }
}

呼叫

Maze maze = Maze.generateMaze(2, 2);

使用构造函数

或更好的

public final class Maze {
  private final boolean[][] mazeArray;

  public Maze(int width, int height) {
    mazeArray = new boolean[width][height];

    for (int x = 0; x < width; x++) {
        mazeArray[x][0] = true;
    }
    for (int y = 0; y < height; y++) {
        mazeArray[0][y] = true;
    }
  }
}

,使用构造函数:并在main中这样称呼它:

Maze maze = new Maze(2, 2);

工厂

您仍然可以将其与出厂方法相结合,如果您真的想要。但是创建逻辑应该在(可能private)构造函数中:尽管如此,

public final class Maze {
  private final boolean[][] mazeArray;

  private Maze(int width, int height) {
    mazeArray = new boolean[width][height];

    for (int x = 0; x < width; x++) {
        mazeArray[x][0] = true;
    }
    for (int y = 0; y < height; y++) {
        mazeArray[0][y] = true;
    }
  }

  public static Maze generate(int width, int height) {
    return new Maze(width, height);
  }
}

请将其称为:

Maze maze = Maze.generate(2, 2);

Walker

现在,您需要一个Walker类的实例,并在此上调用该方法,让它成为您刚刚产生的迷宫:

Maze maze = new Maze(2, 2);
Walker walker = new Walker();

walker.walk(maze);

Explanation

There are several basic OOP mistakes here.

First of all, why do you even create an instance of the Maze class when your generateMaze class is static and returns the maze as instance of boolean[][] instead of Maze. You probably intended to have the array as a field of the class instead and not access the array directly but via a maze instance.

Next, the walk method is non-static and part of Walker instances. So you would need to create an instance of that class and call the method on that instance.


Maze generation

You probably intended to do this instead:

public final class Maze {
  // Arrays as field of maze instances
  private boolean[][] mazeArray;

  // return maze instance instead of array
  public static Maze generateMaze(int width, int height) {
    // create maze instance
    Maze maze = new Maze();
    // manipulate array of that maze instance
    maze.mazeArray = new boolean[width][height];

    for (int x = 0; x < width; x++) {
        maze.mazeArray[x][0] = true;
    }
    for (int y = 0; y < height; y++) {
        maze.mazeArray[0][y] = true;
    }

    // return the maze, not its array
    return maze;
  }
}

with a call like

Maze maze = Maze.generateMaze(2, 2);

Constructor

Or even better, use a constructor:

public final class Maze {
  private final boolean[][] mazeArray;

  public Maze(int width, int height) {
    mazeArray = new boolean[width][height];

    for (int x = 0; x < width; x++) {
        mazeArray[x][0] = true;
    }
    for (int y = 0; y < height; y++) {
        mazeArray[0][y] = true;
    }
  }
}

And call it like this in your main:

Maze maze = new Maze(2, 2);

Factory

You can still couple that with a factory method, if you really want. But the creation logic should be in a (possibly private) constructor nonetheless:

public final class Maze {
  private final boolean[][] mazeArray;

  private Maze(int width, int height) {
    mazeArray = new boolean[width][height];

    for (int x = 0; x < width; x++) {
        mazeArray[x][0] = true;
    }
    for (int y = 0; y < height; y++) {
        mazeArray[0][y] = true;
    }
  }

  public static Maze generate(int width, int height) {
    return new Maze(width, height);
  }
}

calling it like:

Maze maze = Maze.generate(2, 2);

Walker

Now, you need an instance of the Walker class and call the method on that, giving it the maze you just generated:

Maze maze = new Maze(2, 2);
Walker walker = new Walker();

walker.walk(maze);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文