0

How do I set up an 2d array (array of arrays in java)?

    dmap = new sq[255][255];
    for (int y = 0; y < 255; ++y)
        for (int x = 0; x < 255; ++x)
            dmap[x][y] = new sq();

where sq is my other class doesn't work well- I get a long hang (2 mins) and no logging or printfs appear in Eclipse debug views (console + log).

2
  • 3
    Unless new sq() hangs, there's no problem in your code. Commented Jul 1, 2012 at 3:15
  • 1
    The only possible issue is that this should be [y][x] instead of [x][y] in the inner loop. See stackoverflow.com/questions/9936132/…. Commented Jul 1, 2012 at 3:48

1 Answer 1

1

At first You must initialize first dimension of array and then go to next, here is the right code:

sq dmap[][] = new sq[256][];
for (int x = 0; x < 255; ++x){
   dmap[x] = new sq[256];
   for(int y = 0 ; y < 255 ; ++y){
      dmap[x][y] = new sq();
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.