Arrays
This chapter introduces arrays, a common multi-dimensional data structure used to store data of the same type. (Note that a class is a "data type" in this sense.) Arrays have some number of dimensions and a number of elements in each dimension. These are established when the array is created. The range of the array's index in each dimension goes from zero to the number of that dimension's elements minus one. A for loop is commonly used to initialize, manipulate, and access the values in an array. Here, we treat one-dimensional arrays, sometimes called vectors.
3. Using Arrays
Answer:
103
Using Arrays
Every
cell of an array holds a value of the same type. So, for example, you can have an array of int
s, an array of double
s, and so on.
You can have an array of object references. This is discussed in a later chapter.
The "before" array in the picture holds data of type int
. A cell of this array can be used anywhere a variable of type int
can be used. For example,
data[3] = 99 ;
works just like an assignment to an int
variable. After it has been executed, the array looks like the "after" array in the picture.
The value in cell 3 of the array has been changed.
Question 3:
What do you suppose is the value of the arithmetic expression:
data[2] + data[6]