-4

I am wondering why we need two dimensional arrays. I just learned them, but I don't find it helpful to use. Please answer! Kindly appreciate all of your answers.

João
  • 331
  • 1
  • 3
  • 17
Predator
  • 17
  • 2
  • 8
    ... if you have multi-dimensional data? – Matt Jul 19 '14 at 14:31
  • 3
    One example would be a browser game, where u have a map. Now u need to mark the locations of items on the map by using x/y. Or, an array of records, or...just do development, and u will discover yourself where u need it – Itay Moav -Malimovka Jul 19 '14 at 14:32
  • 2
    I wonder why it gets so many down-votes. It is a good question of a beginner. – akonsu Jul 19 '14 at 14:32
  • two dimensional arrays are just a syntactic sugar, a convenience that can be used to program matrices, or any other two dimensional data that you can access using two indices. – akonsu Jul 19 '14 at 14:37
  • 1
    @akonsu Because it is way too broad and call for tons of example-answers ? (btw, I didn't downvote because I don't feel it was necessary here, but I can understand) – Clément Malet Jul 19 '14 at 14:38
  • As a beginner, it is always hard to ask questions. Because I don't understand the thing clearly. So hard to get helps around here because I don't know if my question is good enough to be posted. – Predator Jul 20 '14 at 02:32
  • @Predator: Don't be put off. :-) Having read through [the Help Center](http://stackoverflow.com/help), if you have a question, something that you're running into in your work or study, make it as clear, specific, and complete as you can and post it. (Leave off meta content, like "Please answer! Kindly appreciate all of your answers.") If it's not "good enough" at that point, the community will let you know. I'm with akonsu on this, btw, I don't see why this question got received as it was. It's a bit broad, but sometimes a question *is* a bit broad. – T.J. Crowder Jul 21 '14 at 09:43

1 Answers1

4

JavaScript doesn't have two-dimensional arrays, but it has something very similar: Arrays of arrays. The reason we need two-dimensional arrays or arrays-of-arrays and such is that we use them to represent two-dimensional things. For instance, in Chess, the game board is two-dimensional: It has width and depth (or height, depending on how you want to think of it). So it would make sense to represent it in a way that lets us do [x][y] indexing on it:

var board = [
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
];

That gives me a blank 8x8 chess board. If I want to put a P for a white pawn at position 6,2, I'd do this:

board[6][2] = 'P';

giving me this:

[
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', 'P', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
]

That's much easier than keeping track of things in a one-dimensional array and always multiplying by the width of the board. Consider the one-dimensional alternative:

var board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];

If I want to put a P on 6,2, I have to do this:

board[(6*8)+2] = 'P';

For true two-dimensional arrays, that's primarily a matter of convenience for the programmer.

But for arrays of arrays, it's more interesting than that: The arrays inside the outer array can be different lengths, or (with most languages that do arrays-of-arrays) be missing entirely:

var anArrayOfArrays = [
    [1, 2, 3, 4],
    [1, 2],
    null,
    [1, 2, 3, 4, 5, 6, 7, 8]
];

This can be very handy — not, of course, for representing chess boards. :-) But for representing things that aren't box-shaped. Arrays-of-arrays can do the job of two-dimensional arrays, but they can do other interesting jobs too.

Of course, it doesn't end there. If we want to represent three-dimensional things, we can use three-dimensional arrays (or arrays of arrays of arrays). Star Trek fans might use the chess example again here. ;-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875