int a;

a = 4;

int[] b; // declaration of b a name for an array of integers

Besides the type of the values that the variables are supposed
to help store we still have four kinds variables in Java:

(a) local variables (in methods)
(b) parameters
(c) instance variables (custom initialization usually constructors)
(d) static variables (part of the class available when the class is)

The (c) and (d) type of variables come with default values when they
are declared. 

An array is very much like a book:

-- it has a name
-- you access individual elements (pages) by name and numbers

The String[] args in main is a parameter of String array type. 

import java.util.Arrays; 

public class One {
  public static void main(String[] args) {
    int[] a; // declare the variable 
    a = new int[10]; // allocate ten slots in the array 
    a[8] = -9; // initialize one element 
    System.out.println( a ); // output resembles: [I@132e4c5a
    String output = Arrays.toString( a ); 
    System.out.println( output ); 
  }
}

The toString method in Arrays is converting an array into a String. 

import java.util.Arrays; 

public class One {
  public static void main(String[] args) {
    int[] a = {4, 3, 2, 5, 6, 1}; 
    // declaration, allocation and initialization all at once 
    System.out.println( Arrays.toString( a ) ); 
    Arrays.sort( a ); // order of elements in a changes 
    System.out.println( Arrays.toString( a ) ); 
    
  }
}

Heads-up: you will define your own sort and toString in a future homework.

Summary of what we learned so far: 

  -- we declare, allocate, initialize arrays
  -- we use specialized methods to print, sort them

Are arrays good, bad, easy or hard to use? 

import java.util.Arrays; 

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

    int[] a = {4, 2, 7, 5, 1, 3}; 
    //         0  1  2  3  4  5 
    System.out.println( "a[0] = " + a[0] ); // 4
    System.out.println( "a[1] = " + a[1] ); // 2
    System.out.println( "a[2] = " + a[2] ); // 7
    System.out.println( "a[3] = " + a[3] ); // 5
    System.out.println( "a[4] = " + a[4] ); // 1
    System.out.println( "a[5] = " + a[5] ); // 3
    // System.out.println( "a[6] = " + a[6] ); // run-time exception
    // System.out.println( "a[-1] = " + a[-2] ); // run-time exception 

  }
}

Notice we have pointers in Java just not pointer arithmetic.

So we're like C but more conservative. 

If you want to allocate an array you can specify the size at run-time
and that's fine. Any size array is available to you but you're stuck with
that size after that. 

import java.util.Arrays; 
import java.util.Scanner; 

public class One {
  public static void main(String[] args) {
    int[] a; 
    Scanner tyler = new Scanner(System.in); 
    System.out.print("What size array do you want: "); 
    int n = Integer.parseInt(tyler.nextLine());
    a = new int[n]; 
    System.out.println( Arrays.toString( a ) );     
    
  }
}

In the process we realize that we can have an array with zero elements. 

What is it good for? It shows that that's what we collected thus far. 

import java.util.Arrays; 
import java.util.Scanner; 

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

Now let's start this experiment:

import java.util.Arrays; 

public class One {
  public static void main(String[] args) {
    int[] a = new int[] { 6, 2, 8 }; 
    System.out.println( Arrays.toString( a ) ); 
    System.out.println( a.length ); // 3 
    // how do I squeeze a 5 in this array? 
    a[2] = 5; // but I don't want to lose the 8
    System.out.println( Arrays.toString( a ) );
    // a[3] = 9; // not possible the allocation is final  
    
  }
}

How can I stretch an array? 

import java.util.Arrays; 

public class One {
  public static void main(String[] args) {
    int[] a = new int[] { 6, 2, 8 }; 
    System.out.println( Arrays.toString( a ) ); 
    int[] b = new int[ a.length + 1 ]; // b is one longer than a 
    System.out.println( Arrays.toString( a ) ); 
    System.out.println( Arrays.toString( b ) ); 
    for (int i = 0; i < a.length; i++) {
      b[i] = a[i];  
    }
    System.out.println( Arrays.toString( a ) ); 
    System.out.println( Arrays.toString( b ) ); 
    b[b.length - 1] = -2; 
    System.out.println( Arrays.toString( a ) ); 
    System.out.println( Arrays.toString( b ) ); 
    
    a = b; 
    System.out.println( Arrays.toString( a ) ); 
    
  }
}

This is tedious. 

Fortunately there's a function in Arrays that simplifies this.

import java.util.Arrays; 

public class One {
  public static void main(String[] args) {
    int[] a = new int[] { 6, 2, 8 }; 
    System.out.println( "a = " + Arrays.toString( a ) ); // no surprises here 
    
    int[] b = Arrays.copyOf(a, a.length + 1); // very useful function 
    // b is a new array with a in its a.length first slots plus a 0 in it
    b[b.length-1] = -2;
    System.out.println( "b = " + Arrays.toString( b ) ); // b is finished!

    System.out.println( "a still... " + Arrays.toString( a ) ); // at this stage a unchanged 
    a = b;     
    System.out.println( "a now: " + Arrays.toString( a ) ); // a points to b
  }
}

Now the motivation:

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

public class One {
  public static void main(String[] args) {
    Scanner tyler = new Scanner(System.in); 
    ArrayList<Integer> a; // declaration
    a = new ArrayList<Integer>(); // initialization 
    System.out.println( a ); 
    while (true) {
      System.out.print("Type: "); 
      String line = tyler.nextLine();
      if (line.equalsIgnoreCase("bye")) {
        break;  
      }
      Integer number = Integer.parseInt( line ); 
      a.add( number ); 
      System.out.println( a ); 
    }
    System.out.println("Thanks, here's your array list sorted: "); 
    Collections.sort( a );     
    System.out.println( a ); 
    
  }
}

So this is like your Homework Three only for it you need to use

  -- arrays of int (int[] )
  -- Arrays.copyOf(..., ...)
  -- Arrays.sort(...)
  -- Arrays.toString(...)

Good luck. Assignment is due Tue 09/22 @11:59pm in OnCourse. 

--