Creating an array using the new operator

 

The array examples we’ve considered so far assume that we know the count and the values of the array elements at the time of program creation.  This, unfortunately, is a limiting assumption for any program that is meant to process a collection of data entered by the user.  In this case, we can use the new operator to create an array.  The new operator requires us to specify how many elements contains.  The following statement creates an array of 10 Strings called names:

 

      String[] names = new String[10];

 

The expression to the right of the assignment operator instructs Java to create 10 Strings as designated inside the square brackets.  A statement to create an array of 5 doubles called measurements would be:

 

      double[] measurements = new double[5];

 

Notice that the type on both sides of the ‘=’ must be the same (String and String, double and double).  After this statement executes, the array can be represented as:

 

      measurements     

 

 


0.0

0.0

0.0

0.0

0.0

                    0          1           2           3            4

 

Since no values have been designated, Java assigns the default value for each data type to each element.  For double, it’s 0.0. 

 

The size of an array can also be specified using a variable.  As an example, suppose we’d like to create a program that prompts the user to enter several numbers.  The program stores the values in an array, and then prints them out in reverse order.  To determine how big the array will be, we will first prompt the user to tell us how many numbers he or she will enter.  We will create an array with that many elements.  Here’s the code snippet that does just that:

 

      Scanner input = new Scanner(System.in);

      System.out.print(“How many numbers would you like to enter? “);

 

      // numCount contains the value entered

      int numCount = input.nextInt();

 

      // create an array with numCount number of integers

      int[] array = new int[numCount];

 

Once we’ve created the array, we need to repeatedly prompt the user to enter a value to store in the array.  This can be easily done with a loop (not surprisingly), and we will use a variable i as index into the array.  As before i is initialized to 0, and with each loop iteration we place the value entered by the user in array[i].  The code snippet:

 

            for (int i = 0; i < numCount; i++)

      {

            System.out.print(“Enter a number: “);

 

            // value input by user is assigned to ith element in the array

            array[i] = input.nextInt();

      }

 

Looking at the loop condition, we could as well have used i < array.length since it also refers to the number of elements in that array.

 

To print out the array in reverse order, we will need another loop that visits every element, but starting with the last one (with index array.length-1).  Here’s the code:

 

      System.out.print(“The numbers in reverse order: “);

      for (int i = array.length-1; i >= 0; i--)

            System.out.print(array[i] + “ “);