Skip to content

How to call an 2D – Array which was iniciated in another class?

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? Im affraid 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

}

}

Answer

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);