[Home]Array

HomePage | RecentChanges | Preferences | TableOfContents | AboutTheWiki

An array is one of the very basic data structures. In an array, equally sized elements sequentially follow one another in a collection. In most languages these elements must be of the same type; however some programming languages, such as Perl, allow mixing types in arrays. Any element of this array can then be accessed through an index. Arrays can be multi-dimensional, resulting in a table structure (or a list of lists) instead of just a simple list of elements.

Each element in an array can be used just like a variable of the same type. Accessing an element is as simple as using an index number which can be either a literal number or the value stored in another variable. Some languages provide additional ways for accessing elements of arrays.

Examples

Arrays in C/C++

Here is an example of declaring and using a variable in C:

 int array[3];

array[0] = 0; array[1] = 2; array[2] = 3;

array[0] = array[1] + array[2];

Here an array of four integers is declared. Each element is accessed by an index and set to some value. The result stored in array[0] would obviously be 5.

Arrays in Ada

 myarray : array(1..3) of integer;
 ...
 myarray(1) := 0;
 myarray(2) := 2;
 myarray(3) := 3;

myarray(1) := myarray(2) + myarray(3);

The value in myarray(1) is 5.

Arrays in Perl?

 my @array;

$array[0] = 0; $array[1] = 2; $array[2] = 3;

$array[0] = $array[1] + $array[2];

The value stored in $array[0] is 5.


HomePage | RecentChanges | Preferences | TableOfContents | AboutTheWiki
This page is read-only | View other revisions
Last edited January 27, 2005 2:22 pm CDT by BradDaBug (diff)
Search:
Creative Commons License
All articles licensed under a Creative Commons License.