Here are the notes we took this morning in class:

int a; // declare a variable 

a = 4; // initialize it 

System.out.println( a ); // print it

int[] a; // declare a variable of type array

Allocating arrays can be done as in the example below: 

class One {
  public static void main(String[] args) {
    int[] a;
    int b[];
    a = new int[20];
    System.out.println( a );
    b = new int[10];
    System.out.println( b );
    for (int elem : b ) {
      System.out.print( elem + " " );
    }
    System.out.println();

  }
}

The example also shows

(a) how we can use the enhanced for loop to print arrays. 

In the example below we use two more methods: 

(b) the traditional for to print unidimensional arrays

(c) the java.util.Arrays.toString(...) method that produces a String

class One {
  public static void main(String[] args) {
    int[] a;
    int b[];
    a = new int[20];
    System.out.println( a );
    b = new int[10];
    System.out.println( b );
    b[b.length-1] = -1;
    for (int elem : b ) {
      System.out.print( elem + " " );
    }
    System.out.println();

    for (int i = 0; i < b.length; i++) {
      System.out.print( b[i] + " " );
    }
    System.out.println();

    String bString = java.util.Arrays.toString( b );
    System.out.println( bString );
  }
}

Here's how we can demonstrate that a and b are references. 

import java.util.*;

class One {
  public static void main(String[] args) {

    int[] a, b;
    a = new int[10];
    b = a;
    b[3] = 6;
    System.out.println( Arrays.toString( a ) );
    System.out.println( Arrays.toString( b ) );

  }
}

The actual elements are in fact shared. 

Here are two methods from the minute paper, defined: 

import java.util.*;

class One {
  public static void main(String[] args) {

    int[] a, b;
    a = new int[10];
    b = a;
    b[3] = 6;
    System.out.println( Arrays.toString( a ) );
    System.out.println( Arrays.toString( b ) );
    a = One.generate(10, 20);
    System.out.println( Arrays.toString( a ) );
    a = Arrays.copyOf(a, 23);
    System.out.println( Arrays.toString( a ) );

  }

  public static int generate(int value) {
     return (int) (Math.random() * value);
  }

  public static int[] generate(int value, int size) {
     int[] result = new int[size];
     for (int i = 0; i < result.length; i++) {
        result[i] = One.generate(value);
     }
     return result;
  }
}


--