Define variables: 

  int a; 

  a = 6; 

  a = (int) (Math.random() * 4); // a is one of {0, 1, 2, 3} 

  String b = "something"; 

Strings have a dimension in them, but they are immutable. 

  double c = 4.5;

  boolean d = true && !false;

All of these hold one single value (except for Strings but they're immutable). 

The good news is that Java provides the ability to define, allocate, initialize arrays. 

  int[] a; // declaration that a will refer to a sequence of numbers 

  a = new int[20]; // allocation: a will have 20 slots 

  for (int i = 0; i < a.length; i++)
    a[i] = (int) (Math.random() * 100 - 50);

  System.out.println( a ); // does not look very good 

Multi-dimensional arrays: 

  int[][] b; 

  b = new int[6][10]; 

  for (int i = 0; i < b.length; i++)
    for (int j = 0; j < b[i].length; j++) 
      b[i][j] = (int) (Math.random() * 100 - 50);
  
A two-dimensional array is an array of arrays (of ints). 

A three-dimensional array is an array of arrays of arrays (of ints).

  int[] c = {4, 3, 1, 9, 5, -2, 7};

  int[][] d = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 

  int[][] e = { {1}, {2, 3, 4}, {5, 6} }; 

  int[][][] f = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }, 
                  { {9, 2, 5}, {7, 5, 1}, {8,-1, 0} }, 
                  { {1, 0, 3}, {4, 2, 6}, {7,18, 9} }
                }; 

To get 18 in f you'd have to ask for f[2][2][1].

Let's design the following program:

   write a program that prompts the user for integers one by one

   when the user enters a keyword ("bye") the data entry is stopped

   program records all numbers and prints them in ascending order at the end

To start with let's play in the interactions panel of DrJava:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] a;
> a = new int[6];
> a[2] = 12;
> a
{ 0, 0, 12, 0, 0, 0 }


Now let's assume this is real. Let's write this program: 

public class LectureSix {
  public static void main(String[] args) {
    int[] a; 
    a = new int[6];
    a[2] = 12;
    System.out.println( a ); 
  }
}

This produces: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run LectureSix
[I@490057c


Let's make a change:

import java.util.Arrays;

public class LectureSix {
  public static void main(String[] args) {
    int[] a; 
    a = new int[6];
    a[2] = 12;
    System.out.println( Arrays.toString( a ) ); 
  }
}

Here's what this produces now: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run LectureSix
[0, 0, 12, 0, 0, 0]


Lab Six assignment part I: write your version of the Arrays.toString(int[] ...) method.

Now let's develop this program:

import java.util.*;

public class LectureSix {
  public static void main(String[] args) {
    Scanner speckles = new Scanner(System.in); 
    int[] numbers = new int[0]; 
    while (true) {
      System.out.print("number> "); 
      String input = speckles.nextLine();
      if (input.equals("bye"))
        break; 
      int number = Integer.parseInt( input ); 
      numbers = Arrays.copyOf( numbers, numbers.length + 1 ); 
      numbers[numbers.length - 1] = number;
      System.out.println( Arrays.toString( numbers ) ); 
    }
    // this is the first statement after the loop 
    Arrays.sort( numbers ); 
    System.out.println( Arrays.toString( numbers ) ); 
    System.out.println("Thanks for using this program."); 
  }
}

Here's how this program behaves: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run LectureSix
number>  [DrJava Input Box]
[]
Thanks for using this program.


Here's another run: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run LectureSix
number>  5
[5]
number>  2
[5, 2]
number>  7
[5, 2, 7]
number>  -3
[5, 2, 7, -3]
number>  4
[5, 2, 7, -3, 4]
number>  bye
[-3, 2, 4, 5, 7]
Thanks for using this program.


Lab Assignment Six Part II: define your own 

                    int[] copyOf(int[] ..., int ...)

Now some examples of how this method works. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] a = {1, 2, 3};
> a
{ 1, 2, 3 }
> a[2] = 4
4
> a
{ 1, 2, 4 }
> a[2] = 3
3
> a
{ 1, 2, 3 }
> a[3] = 4
java.lang.ArrayIndexOutOfBoundsException
> a
{ 1, 2, 3 }
> int[] b = Arrays.copyOf( a , a.length + 1 );
Static Error: Undefined name 'Arrays'
> import java.util.Arrays;
> int[] b = Arrays.copyOf( a , a.length + 1 );
> b
{ 1, 2, 3, 0 }
> b[3] = 4
4
> b
{ 1, 2, 3, 4 }
> a = b
{ 1, 2, 3, 4 }
> a
{ 1, 2, 3, 4 }
>

How could you write such a method? 

import java.util.*;

public class LectureSix {
  public static void main(String[] args) {
    System.out.println( Arrays.toString( LectureSix.generate( 10 ) ) ); 
    System.out.println( Arrays.toString( LectureSix.generate(  6 ) ) ); 
    System.out.println( Arrays.toString( LectureSix.generate( 12 ) ) ); 
    System.out.println( Arrays.toString( LectureSix.generate(  8 ) ) ); 
    System.out.println( Arrays.toString( LectureSix.generate(  7 ) ) ); 
  }
  public static int[] generate(int size) {
    int[] a = new int[size]; 
    for (int i = 0; i < a.length; i++) 
      a[i] = (int)(Math.random() * 6 + 1); 
    return a;
  }
}

Here's how it works: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run LectureSix
[5, 3, 1, 1, 1, 2, 6, 5, 1, 3]
[4, 1, 1, 2, 2, 2]
[2, 6, 2, 6, 3, 4, 2, 2, 4, 3, 5, 3]
[5, 4, 2, 5, 3, 1, 1, 6]
[6, 2, 6, 1, 2, 6, 5]


Mark, Logan, Daniel, Jared, Austin, Jacquelyn, Brennan, Adam, Trevor, 
Gabriela, Judy, Walter, Aleksa, Qin, James, Nick, Yiming, Jingzhe, 
Lauren, Zac, Jack, Paul, William, Jon, MAR, Grant, Mohan, Morgan, Alex, 

--