Arrays for Homework 03. 

In Java we have four kinds of variables:

  (a) local variables (inside a method)

  (b) parameters (ingredients to a method)

  (c) instance variables (non-static, defined in class outside methods)

  (d) static variables (marked as static, defined in class outside methods)

Of these only local variables need to be initialized by the programmer. 

The others don't need to (b) or have default values (c, d). 

Otherwise all variables remember values of some type. 

Arrays: use them to remember with just one name more than one value. 

Examples: 

int a = 3; 

int[] b; // name for an array (one-dimensional) 

int[][] c; // name for a two dimensional array 

int[][][] d; // three-dimensional arrays 

b = new int[16]; // allocation 

b[4] = 3; // fifth value of b is set to 3

String[] e = new String[4]; // declaration and initialization of array 

Scanner[] u = new Scanner[10]; 

All elements in the array should be of the same type. 

import java.util.Arrays; 

public class Tuesday {
  public static void main(String[] args) {
    int[] a; // declaration
    a = new int[10]; // allocation
    System.out.println( a ); // [I@7dbab9c8 
    System.out.println( Arrays.toString( a ) ); // actually see elements
    for (int i = 0; i < a.length; i = i + 1) {
      a[i] = (int) (Math.random() * 6) + 1; // simulate dice
    }
    System.out.println( Arrays.toString( a ) ); // actually see elements

  }
}

This prints:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Tuesday
[I@35a6c02a
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 4, 4, 6, 3, 6, 4, 2, 4, 2]


Here's another program:

import java.util.Arrays; 

public class Tuesday {
  public static void main(String[] args) {
    boolean[] a; // declaration
    a = new boolean[10]; // allocation
    System.out.println( a ); // 
    System.out.println( Arrays.toString( a ) ); // actually see elements
  }
}

Inside the array the slots have default values. 

import java.util.Arrays; 

public class Tuesday {
  public static void main(String[] args) {
    String[] a; // declaration
    a = new String[10]; // allocation
    System.out.println( a ); // 
    System.out.println( Arrays.toString( a ) ); // actually see elements
  }
}

Here's a longer example that also answers questions about two dimensional arrays:

import java.util.Arrays; 

public class Tuesday {
  public static void main(String args[]) {
    int a; 
    int[] b = {4, 2, 3, 6, 5, 1};
    System.out.println( Arrays.toString( b ) ); 
    System.out.println( b.length ); 
    int c[];
    int[][] d; // two dimensional array
    int[] e[] = {{4, 9, 2}, // magic square 
                 {3, 5, 7},
                 {8, 1, 6}}; 
    System.out.println( e ); 
    System.out.println( Arrays.toString( e[1] ) ); 
    int f[][] = { {3, 2}, 
                  {2, 1, 5, 3}, 
                  {7, 6, 2}}; 
    System.out.println( f.length ); // 3
    System.out.println( f[0].length ); // 2
    System.out.println( f[1].length ); // 4
    System.out.println( f[2].length ); // 3    
    for (int row = 0; row < f.length; row = row + 1) {
      System.out.println( Arrays.toString( f[row] ) );  
    }
    for (int row = 0; row < f.length; row = row + 1) {
      for (int column = 0; column < f[row].length; column = column + 1) {
        System.out.print(f[row][column] + " ");  
      }
      System.out.println(); 
    }
    
    
  }
}

What do we need for Homework 03: 

  (a) print unidimensional arrays (java.util.Arrays.toString(...))

  (b) sort unidimensional arrays (java.util.Arrays.sort(...))

  (c) find a way to squeeze in new values (stretch the array, make it grow)

import java.util.Arrays; 

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

    int[] b = {4, 2, 3, 6, 5, 1};
    String output = Arrays.toString( b ); 
    // http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-int:A-
    System.out.println( output );     
    // http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-int:A-    
    // System.out.println( Arrays.sort( b ) ); // not good
    Arrays.sort( b ); 
    System.out.println( Arrays.toString ( b ) );     
    
  }
}

Now the last method. 

In a future assignment you will have to define these three functions:

  (a) toString

  (b) sort

  (c) copyOf 

import java.util.Arrays; 

public class Tuesday {
  public static void main(String args[]) {
    int[] a = new int[] {5, 3, 4, 2, 6};
    System.out.println( Arrays.toString( a ) ); 
    // now I want to add one more element at the end of a, a 1
    int[] b = new int[a.length+1]; 
    System.out.println( Arrays.toString( b ) ); 
    for (int i = 0; i < a.length; i = i + 1) {
      b[i] = a[i];  
    }
    System.out.println( Arrays.toString( b ) ); 
    b[b.length-1] = 1; 
    System.out.println( "b now " + Arrays.toString( b ) ); 
    System.out.println( "a still " +  Arrays.toString( a ) ); 
    a = b; // I now forget the older a and call b the new a
    System.out.println( "a now becomes " +  Arrays.toString( a ) ); 
  }
}

This whole process is accomplished by the method Arrays.copyOf

http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOf-int:A-int-

import java.util.Arrays; 

public class Tuesday {
  public static void main(String args[]) {
    int[] a = new int[] {5, 3, 4, 2, 6};
    System.out.println( "a = " + Arrays.toString( a ) ); 
    // now I want to add one more element at the end of a, a 1
    // http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOf-int:A-int-
    int[] b = Arrays.copyOf(a, a.length + 1); 
    b[b.length-1] = 1; 
    a = b; 
    System.out.println( "a = " + Arrays.toString( a ) ); 
  }
}

I can still simplify it a bit:

import java.util.Arrays; 

public class Tuesday {
  public static void main(String args[]) {
    int[] a = new int[] {5, 3, 4, 2, 6};
    System.out.println( "a = " + Arrays.toString( a ) ); 
    // now I want to add one more element at the end of a, a 1
    // http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOf-int:A-int-
    a = Arrays.copyOf(a, a.length + 1); 
    a[a.length-1] = 1; 
    System.out.println( "a = " + Arrays.toString( a ) ); 
  }
}

So we're done. 

import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.Collections; 

public class Tuesday {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in); 
    
    ArrayList<Integer> a; // declaration of a 
    a = new ArrayList<Integer>(); 
    System.out.println( a ); 
    
    while (true) {
      System.out.print("Type: "); 
      String line = s.nextLine(); 
      if (line.equals("bye"))
        break; 
      int number = Integer.parseInt( line ); 
      a.add(number); 
      System.out.println( a ); 
      
    }    
    Collections.sort( a ); 
    System.out.println( a ); 

  }
}

This is the same problem implemented with array lists, for motivation. 

--