30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 Arrays 247<br />

location or element in an array, we specify the name of the array and the position number<br />

of the element <strong>to</strong> which we refer. Position numbers are values that indicate specific locations<br />

within arrays.<br />

Figure 7.1 depicts an integer array named numberArray. This array contains 12 elements,<br />

any one of which can be referred <strong>to</strong> by giving the name of the array followed by the<br />

position number of the element in parentheses (). The first element in every array is the<br />

zeroth element. Thus, the first element of array numberArray is referred <strong>to</strong> as number-<br />

Array(0), the second element of array numberArray is referred <strong>to</strong> as number-<br />

Array(1), the seventh element of array numberArray is referred <strong>to</strong> as<br />

numberArray(6) and so on. The ith element of array numberArray is referred <strong>to</strong> as<br />

numberArray(i - 1).<br />

The position number in parentheses more formally is called an index (or a subscript).<br />

An index must be an integer or an integer expression. If a program uses an expression as an<br />

index, the expression is evaluated first <strong>to</strong> determine the index. For example, if variable<br />

value1 is equal <strong>to</strong> 5, and variable value2 is equal <strong>to</strong> 6, then the statement<br />

numberArray(value1 + value2) += 2<br />

adds 2 <strong>to</strong> array element numberArray(11). Note that an indexed array name (i.e., the<br />

array name followed by an index enclosed in parentheses) is an lvalue—it can be used on<br />

the left side of an assignment statement <strong>to</strong> place a new value in<strong>to</strong> an array element.<br />

Name of array (note<br />

that all elements of this<br />

array have the same<br />

name, numberArray)<br />

Position number (index or<br />

subscript) of the element<br />

within array numberArray<br />

numberArray(0)<br />

numberArray(1)<br />

numberArray(2)<br />

numberArray(3)<br />

numberArray(4)<br />

numberArray(5)<br />

numberArray(6)<br />

numberArray(7)<br />

numberArray(8)<br />

numberArray(9)<br />

numberArray(10)<br />

numberArray(11)<br />

Fig. Fig. 7.1 7.1 Array consisting of 12 elements.<br />

-45<br />

6<br />

0<br />

72<br />

1543<br />

-89<br />

0<br />

62<br />

-3<br />

1<br />

6453<br />

78

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!