2

for university I was working on a game with several friends where we had to load levels from a .txt, save it in a data format of our choosing and obviously display it on the screen. We decided on a two-dimensional char array, which I -- as I was programming the level parser -- filled row by row. To make it more descriptive, an excerpt of the code I used:

unconverted = "#####\n" + "#___#\n" + "#S>X#\n" + "#___#\n" + "#####" // sample data
while(z < unconverted.length())
    {
        current = unconverted.charAt(z);
        if(current == 'S' || current == 'X' || current == '<' || current == 'v' || current == '>' || current == '^' || current == '_' || current == '#' ||current == 't')
        {
            level[x][y] = current;
            x++; 
        }
        else if (current =='\n')
        {
            x=0;
            y++;
        }
        else
        {
            System.out.println("Level null because of an unrecognised character");
            level = null;
            return level;
        }
        z++;
    }

It worked fine so far, but we didn't really expect a problem to come up. The others didn't know much beyond that they would get a char[][], which seemed like enough information ... but it wasn't! The problem that appeared was that at various points in the game logic as well as in the GUI, the array was read either per row or per column, meaning that for example the display of the level was turned on it's head.

This obviously cost us quite a lot of time fixing the code to make it all consistent again. So for the future I would like some advice on what is generally accepted to be the "right" way, if there is any: filling and accessing a 2-dimensional array by row or by column? Furthermore, is there any performance difference whatsoever between those two methods? My common sense would say that filling the second dimension first

Thank you very much!

Haris
  • 778
  • 2
  • 9
  • 20
  • Yes, it is. But it was meant as a general question independent of programming language. The code was just meant to make my question easier to understand. – Haris Feb 28 '12 at 02:09
  • Okay, that makes sense. Would you mind posting some sample data? – Alan Feb 28 '12 at 02:19

2 Answers2

2

There isn't a "right" way. Either one can be best, depending on context.

Think about binary trees. Which is the "right" way: breadth first, depth first, or something else? (Hint: it depends on context.)

Here's another example: You can think of relational tables in databases in such a way that each row is a tuple that represents an entity. But some people, like Michael Stonebreaker, think that column based representations can be advantageous in - wait for it - certain contexts.

One more: If you have a matrix with m rows and n columns, which way is better to populate it? (Hint: it doesn't matter.) When you do LU decomposition, it's usually done by iterating over rows. You find the pivot, divide all the entries in that row by it, and then eliminate over all the rows under the pivot. Working by row makes sense in that context.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

Intro: that's how 2D arrays are laid out in memory.

So the cache-efficeint way is to iterate the last index while keeping the same first index. This may make sense for huge arrays of primitive types, e.g in numerical algorithms.

I suppose that in your case no difference will be observed. Use the most logical way.

Community
  • 1
  • 1
9000
  • 39,899
  • 9
  • 66
  • 104