Topic 8: Arrays

Part One

(8.1) Array elements

(8.2) Declaring and using arrays

Example

Bounds checking

Example

Example

Alternate array syntax

int [] x; // preferred
int x[];  // also permitted but less perspicuous

The type of x is int [].

Initializer lists

Examples

Arrays as parameters

  1. Entire array as parameter:

    foo(x);
    
    • What is copied?
    • What can the called method mutate?
  2. New array object as parameter:

    foo(new int[] {7, 21, 23});
    
  3. Array element as a parameter:

    bar(x[5]);
    
    • Same questions

(8.3) Arrays of Objects

An array can be declared with either primitive or reference type elements.

int [] age = new int[4];
String [] size = new String[4];

Primitive Type Elements (1)

int [] age = new int[4];
Array of int, before initialization

Array of int, before initialization

Primitive Type Elements (2)

age[0] = 7; age[1] = 25; age[2] = -3; age[3] = 54;
Array of int, after initialization

Array of int, after initialization

Reference type elements (1)

String [] size = new String[4];
Array of String, before initialization

Array of String, before initialization

Reference type elements (2)

size[0] = "large"; size[1] = "small"; 
size[2] = "medium"; size[3] = "x-large";
Array of String, after initialization

Array of String, after initialization

Example

Example

(8.4) Command-line arguments

The mystery of public static void main (String [] args)

Example

Another Example

Write a program which adds the numbers on its command line and prints the sum.

Solution

Variable length parameter lists

Syntactic sugar

Other Parameters

Example

Discussion

Topic 8: Arrays

Part Two

(8.6) Two-dimensional arrays

Example

Example

Multidimensional arrays

Java Collections Framework (JCF)

Generic Types Example

ArrayList<Musician> band = new ArrayList<Musician>();

Advantages:

  1. Stricter type checking for elements put into collection
  2. Eliminates need for cast when retrieving elements

Without Generic Types

An ArrayList can store any Object as an element.

ArrayList band = new ArrayList();
band.add(new Musician("J. S. Bach"));
band.add(new String("abc")); // undetected error

// retrieval
Object obj = band.get(0);
if (obj instanceof Musician) {
    Musician mus = (Musician) obj; 
    ... 
} 
else 
    System.err.println("Error: ...");

With Generic Types

An ArrayList can store only Musician elements.

ArrayList<Musician> band = new ArrayList<Musician>();
band.add(new Musician("J. S. Bach"));
band.add(new String("abc")); // compiler detects error

// retrieval
Musician mus = band.get(0); // safe and simple

ArrayList Efficiency

(In)efficiency of insert and delete far from the rear of the list, and of expanding and contracting the array.

Efficient Collections

(8.7) Polygons and polylines

Drawing Methods

Polygon class

Polygon class, continued

Example

Exercise

Revise the Rocket example using Polygon objects

(8.8) Mouse events

One class and two interfaces

MouseListener Interface

Methods with MouseEvent argument (Fig. 8.7):

mousePressed(event) // mouse button down
mouseReleased(event) // mouse button up
mouseClicked(event) // down then up
mouseEntered(event) // mouse enters component
mouseExited(event) // mouse leaves component

To implement MouseListener, a class must define all five!

MouseMotionListener Interface

Methods:

mouseMoved(event) // mouse moves
mouseDragged(event) // mouse moves with button down

MouseEvent class

Point class

Examples

Example

(8.9) Key events

KeyListener Interface

Methods (Fig. 8.8):

Differences

Autorepeat

Focus

Example

Discussion