Wednesday, 17 December 2014

// The "TablesAndArraysExample3" class.  Page 311.
// (from the Carter book)

// Example 3:

// Java has an alternative form of array declaration
// that allows us to initialize an array with any values
// we like.  The statement below declares an int array
// called "primes", with enough space to hold only 10
// prime numbers and assigns prime number values.
//
// 1. Notice in this example the absence of the keyword 'new'
// and the use of brace brackets rather than square brackets
// on the right side of the assignment operator.
// 2. In addition, the size of the array is not stated explicitly;
// Java knows that the array is of length 10 because there are
// 10 values inside of the brace brackets.
// 3. The values inside of the brace brackets need not be constants;
// They can be expressions which will be evaluated at the time the
// program is executed.
// 4. The only restriction on these expressions is that they all be
// of the same type, since arrays are collections of values of the
// same type, in this case, int.

public class TablesAndArraysExample3
{
    public static void main (String[] args)
    {
        int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

    } // main method
} // TablesAndArraysExample3 class

No comments:

Post a Comment