Source of Initialize.java


  2: import java.util.Scanner;

  4: /**
  5:  * A program to demonstrate how an array can be created with its elements
  6:  * filled in and its size calculated automatically.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class Initialize {

 12:     public static void main(String[] args) {
 13:         // create the variables...
 14:         Scanner kbd = new Scanner(System.in);
 15:         // ...giving arr its values (and size, automatically)
 16:         int[] arr = new int[] {2, 3, 5, 7, 11};

 18:         // introduce yourself
 19:         System.out.print("\n\n"
 20:                 + "A program to initialize an array.\n\n");


 23:         // report on the array we created
 24:         System.out.println("The size of arr is " + arr.length);
 25:         System.out.println("The elements of arr are ");
 26:         for (int i = 0; i < arr.length; i++) {
 27:             System.out.print(arr[i] + " ");
 28:         }
 29:         System.out.println();

 31:         // change arr to a new size with new values
 32:         arr = new int[] {13, 17, 19, 23};
 33:         System.out.println("The size of arr is " + arr.length);
 34:         System.out.println("The elements of arr are ");
 35:         for (int i = 0; i < arr.length; i++) {
 36:             System.out.print(arr[i] + " ");
 37:         }
 38:         System.out.println();

 40:         System.out.print("\n\n");
 41:     }

 43: }