Introduction to Arrays

How to Define and Use Arrays in Programming

© Guy Lecky-Thompson

Language independent guide to arrays, including how they are defined, accessed and used, in general terms.

Introduction

This article is a language independent guide to arrays. It is designed to be an introductory article for programmers starting out with any language. However, the reader should be conversant with the subject of variables and data types, or at least understand that a variable is a place in memory where a value can be stored, retrieved and updated.

Having read the article, the reader should be able to find the relevant details in their language guide for actually implementing an array, and know when to use them, and what they can be used for.

What is an Array?

Arrays are a vital part of programming, as they allow the programmer to store more than one value in a variable, whilst retaining a single reference. If we think of a variable as a single slot in memory (or a box) that can contain data of a certain type - number, character, etc. - then an array is the equivalent of a box divided into partitions, each containing a piece of data. Of course, because the array box is storing more information than a single variable box, it is much bigger : it needs more memory.

We can use the same name to access the variable, but we need some way to differentiate between the individual slots. To to this we use an index into the array. For example, supposing we have an array that is 100 units wide, we might access the hundredth unit thus:

myArray[99] = 3;

This example also illustrates another feature of most arrays - the index is usually zero based. In other words, the index to the first item is [0] and the index to the last item is [number of elements - 1].

In many programming languages, a string is treated as an array of characters. Usually these arrays are terminated with a null character to indicate the end of the string. This allows software to process strings without knowing the dimension of the array at design time. A collection of strings is still possible, however, by using a multidimensional array.

Multi-Dimensional Arrays

A multi-dimensional array is an array of arrays. If we think of a one-dimensional array as a row of slots in a box, then a two dimensional array is a grid of slots, and a three dimensional array is a cube of slots. Beyond three dimensions it becomes difficult to conceptualize, but theoretically at at least, arrays can have any dimension.

If we wish to represent a simple grid (a chessboard for example), we could define the array as:

myArray[8][8]

The square at row 3, column 5 would be referenced thus:

myArray[3][5] = 1;

We note from this example, that the convention for accessing arrays is often [row, column]. The reason for this is that if we want to store an array of strings, it makes sense to access an individual character in a single string as:

myStringArray[3][1]

This references the second character in the fourth string in the array.

Further Reading


The copyright of the article Introduction to Arrays in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Introduction to Arrays must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo