Arrays are "lists" of related values. Every value in the array is usually of the exact same type and only differentiated by the position in the array. For example, all the quiz scores for a test could be stored in an array with the single variable name: quiz_scores. The first students quiz score would be found in the first "bucket" of the array, the second students score in the "second" bucket, etc.
For general information on data types and arrays see the topic on Data Types.
Arrays are used to store lists of related information. Your shopping list (Type = string); for the names of the students in a class (type = string) for the grades for the first exam (type = numbers).
Note: Array variables should usually be named with "plural" words. For example: grades, students, words.
Arrays have a couple of interesting properties/terminologies that are used with them:
Arrays have a single name, but many values. How do we (the programmer) define which particular value we are interested in? The answer is, we provide a numerical "index".
The index into an array is a number representing which bucket is currently of interest. Consider an array with the following 5 grades : [ 100, 95, 99, 87, 90 ];
The 3rd value in the array is 99.
The idea of a related list of information is so prevalent, that all languages support arrays in one form or another. Thus, the "semantics" of arrays is always the same. The "syntax" and "usage" of arrays can vary. Below we discus the differences between C, Matlab, and Actionscript
%
% Create an array, print the 3rd value in the array, update that value, and print the new value.
%
grades = [ 100, 90, 50 ];
fprintf('the 3rd grade in the array is %f\n', grades(3) );
grades(3) = 70;
fprintf('the updated 3rd grade in the array is now %f\n', grades(3) );
//
// Create an array, print the 3rd value in the array, update that value, and print the new value.
//
int grades[3] = { 100, 90, 50 };
printf("the 3rd grade in the array is %f\n", grades[2] );
grades[2] = 70; // NOTICE 2 means the third value in the array!!!! ZERO BASED!!!
printf("the updated 3rd grade in the array is now %f\n", grades[2] );
/*
* Create an array, print the 3rd value in the array, update that value, and print the new value.
*/
var grades = new Array( [ 100, 90, 50 ] );
trace("the 3rd grade in the array is " + grades[2] );
grades[2] = 70; // NOTICE 2 means the third value in the array!!!! ZERO BASED!!!
trace("the updated 3rd grade in the array is now " + grades[2] );
Remember: in C, Java and Actionscript, arrays are indexed from 0, whereas Matlab is indexed from 1.
Key Idea: the value of bucket in an array and the index of the bucket are two separate things. The value is stored at the location of the index.
Because arrays are "indexed" by numbers, we can "iterate" over an entire array using code such are:
grades = ...; % create an array of grades
for index assigned from 1 to the length of the grades array
print the grade at "index" is grades(index)
end
We use the symbolic variable "index" to reference the current value in the array that we are concerned with.
The size of an array (also known as the length) is the number of buckets in the array that have data in them. Most of the time we will use the symbolic "length" of the array when coding. We seldom will "hard code" an actual number, such as 7, for the length of an array..
Example:
%
% Sum up the values in a variabled named array_of_grades
%
total = 0;
for i = 1 : length(array_of_grades) % the length function tells us the number of values in the array!!!
total = total + array_of_grades(i);
end
//
// Sum up the values in a variabled named array_of_grades
//
float total = 0;
float array_of_grades[100];
int length_of_array = 100; // There is NO length function, so we have to "hard code" length
for (int i=0; i <length_of_array; i++)
{
total = total + array_of_grades[i];
}
//
// Sum up the values in a variabled named array_of_grades
//
var total : float = 0;
var grades : Array = new Array(100); // 100 possible values in array
for (var i:int=0; i <grades.length; i++)
{
total = total + grades[i];
}
Again, to find out the "Size" of an array, you can use the following syntax:
length(name); % provides the number of characters in the string
length(grade_list); % provides the number of numbers in the array
// YOU CANNOT. You must create a variable and must keep track of the
// size yourself
grade_list.length(); // get length of list of grades
grade_list.length;
The capacity of an array is how many values can be stored in it. In languags like Matlab, this number is "expandable" (it can grow larger). In languages like C, this number is "fixed" (it must be set immediately and doesn't change).
For most languages, this number is "hidden" from you and is really only used internally by the computer for efficiency purposes (i.e., by preallocating a large capacity, you don't have to expand the array very often as more data is encountered (but you do waste space in this manner)).
For most purposes, the length (or size) of the array is what is more important.