Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are a couple examples of arrays in familiar situations.</p> <ol> <li><p>You might imagine a 2 dimensional array is as a grid. So naturally it is useful when you're dealing with graphics. You might get a pixel from the screen by saying</p> <pre><code>pixel = screen[20][5] // get the pixel at the 20th row, 5th column </code></pre> <p>That could also be done with a 3 dimensional array to represent 3d space.</p></li> <li><p>An array could act like a spreadsheet. Here the rows are customers, and the columns are name, email, and date of birth.</p> <pre><code>name = customers[0][0] email = customers[0][1] dateofbirth = customers[0][2] </code></pre></li> </ol> <p>Really there is a more fundamental pattern underlying this. Things have things have things... and so on. And in a sense you're right to wonder whether you <em>need</em> multidimensional arrays, because there are other ways to represent that same pattern. It's just there for convenience. You could alternatively</p> <ol> <li><p>Have a single dimensional array and do some math to make it act multidimensional. If you indexed pixels one by one left to right top to bottom you would end up with a million or so elements. Divide by the width of the screen to get the row. The remainder is the column.</p></li> <li><p>Use objects. Instead of using a multidimensional array in example 2 you could have a single dimensional array of Customer objects. Each Customer object would have the attributes name, email and dob.</p></li> </ol> <p>So there's rarely one way to do something. Just choose the most clear way. With arrays you're accessing by number, with objects you're accessing by name.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload