3 An array is a tabular arrangement of values. Values can be retrieved by referring to the array name together with the numeric index of the part of the table storing the desired value. As you may have spotted, by having a loop with a numeric variable we can easily
perform an action on all, or some sub-sequence, of the values stored in the array.
15.2.2 Indexing of array elements As can be seen from the above figure, there are seven elements in the weekDays array.
DEFINITION — element Arrays are composed of a numbered sequence of elements. Each element of an array can bethought of as a row (or sometimes column) in a table of values. The seven elements are indexed (numbered) from zero (0) to six (6). Although it might seem strange to start by numbering the first element at zero, this way of indexing array elements is common to many high-level programming languages (include CC+
and Java, and has some computational advantages over arrays that start at 1.
Note The index of an array element is also known as its subscript. The terms array index and array subscript can be used interchangeably. In this unit we consistently use the term
index for simplicity.
Exercise 1 Answer the following questions
about the weekDays array • What is the first element
•
What is the last element • What is the 4th element
• What is the
value of the first element • What is the value of the 4th element
• What is the element containing String "Monday
• What is the element containing String "Saturday
• What is the index of the element containing String "Monday
• What is the index of the element containing String "Saturday
Share with your friends: