-2
\$\begingroup\$

I'm trying to set one character on 2D array.

map[0][2] = "0";

However, I'm getting

11011
11011
11011
11011
...

I was expecting

11011
11111
11111
11111
...

What am I doing wrong?

another example:

int arraySize = 20;
        String[] blankLine = {"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"};
        String[][] blankM = new String[arraySize][];
        for (int i = 0; i < arraySize; i++) {
            blankM[i] = blankLine;

        }
        blankM[0][0] = "5";
        return blankM;

It gives me in every colum [n][0] 5

\$\endgroup\$
5
  • \$\begingroup\$ I don't see how that's possible. I'm guessing the problem is in how you're reading the map. What does this have to do with game development? \$\endgroup\$ Commented Feb 20, 2014 at 9:17
  • \$\begingroup\$ Well im developing random apearing dungeons, and i have a blank map witch is in 2d array made from string like map[][] = {{"1","1","1"...}, {"1","1","1"...}, ...} and tryng to set in blank map my dungeons from certain positions. And for testing not using for loop but just setting staticly one character and my result is what i wrote as Q. \$\endgroup\$ Commented Feb 20, 2014 at 9:25
  • \$\begingroup\$ This is insufficient detail to solve your problem. It's impossible for a whole column of a 2D array to change if you're only setting one value. Again: Have you ruled out the possibility that the code reading the array is wrong? \$\endgroup\$ Commented Feb 20, 2014 at 9:30
  • \$\begingroup\$ It looks like you are referencing the same "blankLine" multiple times. \$\endgroup\$ Commented Feb 20, 2014 at 9:58
  • \$\begingroup\$ Yes but I setting one character after loop. \$\endgroup\$ Commented Feb 20, 2014 at 10:02

2 Answers 2

0
\$\begingroup\$

Just to take this one step further, lets make a function:

public static String[][] generateMap(int width, int height, String content)
{
    String[][] blankM = new String[height][width];
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++){
        blankM[i][j] = content;
        }
    }
    return blankM;
}

This function can be used to generate a map filled with 1's or any other content so you can generate your maps based on your current needs.

System.out.println(Arrays.deepToString(generateMap(5,6,"1")));
System.out.println(Arrays.deepToString(generateMap(2,3,"IT'S A TRAP")));
//Results in these arrays:
[[1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1]]
[[IT'S A TRAP, IT'S A TRAP],
 [IT'S A TRAP, IT'S A TRAP],
 [IT'S A TRAP, IT'S A TRAP]]
\$\endgroup\$
1
  • \$\begingroup\$ ideone.com/CzZOOy Here it is in action. Try it out \$\endgroup\$ Commented Feb 20, 2014 at 10:37
4
\$\begingroup\$

You need to create a new instance for each item in the array.

String[][] blankM = new String[arraySize][];
for (int i = 0; i < arraySize; i++) {
    blankM[i] = new String[]{"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"};
}
\$\endgroup\$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.