An array is a container that holds a group of data of the same type. An easy way to understand this is to think about food… I love using food examples to explain computer science concepts, because I LOVE EATING!! But anyway, think about Starburst candy. It has a wrapper, which you can think of as the container, and all of the individually colored candies inside represents the contents. A standard array is just one row long. Look at the picture of the starburst candy. We have to remember that we start counting from zero, and we need to make sure that we name each individual item. For instance in the example, the 6th element is represented by starburst[5] so all you have to do is assign the color of the starburst by using the number contained within brackets. This is pretty self explanatory. An important thing to remember is that we cannot try to print something that is not there, for instance, if you try to do this: print "the color of the 12th piece of candy is " + starburst[11]; Just for the record, the last example was written in the Perl programming language.
Above is an example of a 1D array.
The thing to remember is that arrays and other concepts exist in virtually all languages, all you have to figure out is the syntax.
I will talk about 2D arrays soon.
No comments:
Post a Comment