Arrays
Array programming is a difficult but important concept. It gets to
the nitty-gritty of memory allocation in a computer.
What is an array?
Imagine a carton of eggs. You move the eggs by moving the entire
carton, or you can move the eggs one at a time. A carton of eggs
is an individual entity that contains items which can also be addressed
individually. You have "eggs" and then you have egg #1, egg #2, etc. You buy
a dozen eggs at the store in a carton, but when you get home, you open the
carton and move them one at a time into the little egg-shaped
slots inside the refrigerator.
An array is like a carton of eggs, it's a holding place for something. Without
the eggs the carton isn't very important. An array is a holding place
in memory for things. What things? Numbers, letters, other arrays. The eggs are
the items we put in the array. We move items in and out of an array one
at a time just like we move eggs out of a carton one at a time.
Two-Dimentional Arrays as maps and visual grids
Imagine a rough map of the U.S. North East with locations
of large cities placed on a gid, it might look like this:
| 0 | 1 | 2 | 3 | 4 | 6 |
| 1 | CHI | | | | BOS |
| 2 | | | | NY | |
| 3 | | | | DC | |
| 4 | KC | | | | |
| 5 | | | | | |
Chicago is at x = 1, y = 1
Boston is at x = 6, y = 1
New York is at x = 4, y = 2
D.C. is at x = 4, y = 3
Kansas City is at x = 1, y = 4
Let's say we're in New York and we want to go to D.C.,
we need to go south. In this case we need a change in y
and no change in x. x = x + 0 and y = y + 1. Our current
location is x=4,y=2. Adding 1 to y would put us at x=4,y=3 the
location of D.C.
Java array example
Java 2D arrays
C++ Array
More C++