Array
An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.
For example, a search engine may use an array to store Web pages found in a search performed by the user. When displaying the results, the program will output one element of the array at a time. This may be done for a specified number of values or until all the values stored in the array have been output. While the program could create a new variable for each result found, storing the results in an array is much more efficient way to manage memory.
The syntax for storing and displaying the values in an array typically looks something like this:
arrayname[0] = "This ";
arrayname[1] = "is ";
arrayname[2] = "pretty simple.";
print arrayname[0];
print arrayname[1];
print arrayname[2];
The above commands would print the first three values of the array, or "This is pretty simple." By using a "while" or "for" loop, the programmer can tell the program to output each value in the array until the last value has been reached. So not only do arrays help manage memory more efficiently, they make the programmer's job more efficient as well.
First Published: 2008
Last Updated: October 17, 2007