Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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? –  Anko Feb 20 at 9:17
    
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. –  Vadimas Sizikovas Feb 20 at 9:25
    
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? –  Anko Feb 20 at 9:30
    
It looks like you are referencing the same "blankLine" multiple times. –  Arthur Wulf White Feb 20 at 9:58
    
Yes but I setting one character after loop. –  Vadimas Sizikovas Feb 20 at 10:02
add comment

closed as off-topic by Anko, msell, concept3d, Philipp, Nick Wiggill Feb 20 at 19:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Anko, msell, concept3d, Philipp, Nick Wiggill
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 4 down vote accepted

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"};
}
share|improve this answer
    
Thanks for suport ;) –  Vadimas Sizikovas Feb 20 at 10:13
add comment

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]]
share|improve this answer
    
ideone.com/CzZOOy Here it is in action. Try it out –  Blue Feb 20 at 10:37
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.