Hi all!

The past couple times I posted they were all about 2D arrays.. and here I am again with them =( I cannot escape them!

Anyway, the project I'm working on requires a Maze class that holds a 2D array and different types of robots that will find their way through the maze.


Main problem: I just want to copy the 2D array inside Maze class to the 2D array inside Robot class. My biggest trouble is accessing where the robot is (orientation) on the maze. And since the maze itself (inside the Maze class) is a private variable, I cannot seem to access the values of the array itself.

Example:
Below is just a quick written summary of what I have so you can see what I'm tying to do.

public class Maze{
      private String[][] maze;
      private int startX, startY; //declares where the starting point is to place the robot!
    public Maze(){ // fill maze array with "|"}

    public String[][] getMaze(){ return maze; }
    
    //get/set startX/startY functions();
}


public class Robot{
     
     private int x,y;
     private (enum) Orientation; //tells whether the robot's right hand is facing left, right, up, or down on the maze...
     private String[][] newMaze;     


    public Robot(Maze m){
       x = m.getStartX();
       y = m.getStartY();

       newMaze = m.getMaze() //This returns an address - need values to copy to this array!
    }
   
    public void move(){
       //test where the robot is and what is around him in the variable newMaze.
    }

}

This code isn't very pretty but I think it gets the point across. In C++ you can dereference array's to get their values, but I don't know how this would work in Java. There may even be a better way to test where the robot is on the maze, but I'm not sure how.. any ideas on how to fix this?

Thanks in advance!

newMaze contains a copy of a reference (pointer) to the maze array in the instance of Maze, so yo can just use that as in newMaze[i][j] which will reference exactly the same memory location as the original maze[i][j]

When I saw the topic, it was about copying. But I am not sure that what you mean after I saw your comment about dereferencing array. Do you want the maze value inside Robot class to be automatically changed when the maze value changed after the maze values are copied over? If so, you could do what James said, you can assign it directly to your class variable. If not, you have to iterate through each sub in the array and reassign the string value to the Robot maze value.

Sorry I haven't commented back in 2 days.

Ideally I want the maze class to just give the robot a standard maze. The Robot is going to be changing the maze inside the Robot class.

The main assignment was to read in a text file into a 2D array; however, since I don't have the time to code that part, the Maze class is just going to be a default map that never changes.

Hope this makes sense and thanks for the feedback guys!

OK, so the maze class has a "template" maze that the Robot can change, but if a second robot is started it will have the original maze, not the changed one? In that case yes, you need a copy, not just a shared reference to the same maze. You can get clever with the arrays class, but it's probably easier create a new array of the same size, and use a simple nested loop to copy the contents from the original array to the copy array.
Something like:

String[][] getNewMaze() {
   create new String[][] same size as maze
   copy all the data from maze to the new array
   return to new Array
}

^^ That's exactly what I was looking for thank you for your help. Much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.