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.
10. More Complicated Example
Answer:
C:\users\default>java ArrayEg2
cell 3: 0.0
cell 2: 2.98
cell 1: 1.43
More Complicated Example
public class ArrayEg3 { public static void main ( String[] args ) { double[] val = new double[4]; val[0] = 1.5; val[1] = 10.0; val[2] = 15.5; int j = 3; val[j] = val[j-1] + val[j-2]; // same as val[3] = val[2] + val[1] System.out.println( "val[" + j + "] == " + val[j] ); } } |
Question 10:
What does the above program print out?