Arrays

double[] value = new double[10];

int[] squares = {0. 1, 4, 9, 16};

String[] friends = {"Emily", "Bob", "Cindy"};

System.out.println("The value of this data item is " + value[2]);

double[] value = new double[10];
value[10] = 29.95; // ERROR


What elements does the data array contain after the following statements?

double[] data = new double[10];
for (int i = 0; i < data.length; i++) data[i] = i * i;

Answer: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, but not 100


What do the following program segments print? Or, if there is an

error, describe the error and specify whether it is detected at

compile-time or at run-time.

  1. double[] a = new double[10];
    System.out.println(a[0]);

  2. double[] b = new double[10];
    System.out.println(b[10]);

  3. double[] c;
    System.out.println(c[0]);

   Answer:

  1. 0.

  2. a run-time error: array index out of bounds

  3. a compile-time error: c is not initialized