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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.