Creating and Initializing an Array

The application of Fig. E.2 uses keyword new to create an array of 10 int elements, which are initially zero (the default for int variables). Line 8 declares array—a reference capable of referring to an array of int elements. Line 10 creates the array object and assigns its reference to variable array. Line 12 outputs the column headings. The first column contains the index (0–9) of each array element, and the second column contains the default value (0) of each array element.

 1   // Fig. E.2: InitArray.java 2   // Initializing the elements of an array to default values of zero. 3  4   public class InitArray 5   { 6      public static void main( String[] args ) 7      { 8          ...

Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.