Referencing an array element:
x[0] // minimum index
x[5]
x[5] + 1 // part of expression
x[i + 1] // computed index
Java arrays are objects; variables must be declared.
int [] x;
Object [] objs;
Creating arrays:
x = new int[10];
objs = new Object[5];
The for loop is handy:
for (int i = 0; i < x.length; i++)
System.out.println(x[i]);
or
for (int y : x)
System.out.println(y);
ArrayIndexOutOfBoundsExceptionx.length is the number of elements in array x
charAt methodint [] x; // preferred
int x[]; // also permitted but less perspicuous
The type of x is int [].
Example:
int [] x = new int[] {1, 7, -10, 4, 29};
Variations
int [] x =
new int[5] {1, 7, -10, 4, 29}; // syntax error
int [] x = {1, 7, -10, 4, 29}; // not recommended
Entire array as parameter:
foo(x);
New array object as parameter:
foo(new int[] {7, 21, 23});
Array element as a parameter:
bar(x[5]);
An array can be declared with either primitive or reference type elements.
int [] age = new int[4];
String [] size = new String[4];
int [] age = new int[4];

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

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

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

Array of String, after initialization
addDVD inserts new DVD at the end of the list
The mystery of public static void main (String [] args)
String [] args is an array of StringExpects two arguments; run as
java NameTag GREETING NAME
(Replace GREETING and NAME with actual values.)
Write a program which adds the numbers on its command line and prints the sum.
Example:
$ java Adder 10 20 30 40
100
Integer.parseInt(args[i])Design, develop, and test …
Allows calling a method with variable numbers of parameters:
foo(12, 9, 8);
foo(new int [] {12, 9, 8}); // also works
foo(7, 2, -14, 21, 28, 39);
Declaring the method:
public void foo (int ... data) { ... }
Varying parameters must be of same type
data is actually an arrayCompiler transforms method declaration and call into:
public void foo (int [] data) { ... }
foo(new int [] {12, 9, 8});
foo(new int [] {7, 2, -14, 21, 28, 39});
Any other named parameters must precede varying parameters
// okay
public void foo (int a, int b, int ... data)
// wrong
public void foo (int ... data, int a, int b)
Require two subscripts:
t[2][5] // row, column
table.length = number of rowstable[row].length = number of columns in current row{{...}}ObjectsArrayList<Musician> band = new ArrayList<Musician>();
Advantages:
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: ...");
An ArrayList
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
(In)efficiency of insert and delete far from the rear of the list, and of expanding and contracting the array.
java.awt.Graphics class
drawPolygon(xs, ys, n)
fillPolygon(xs, ys, n)
drawPolyline(xs, ys, n)
xs, ys: int [] arrays of coordinatesn: number of points used
Constructors
Polygon()
Polygon(xs, ys, n)
Public fields
int [] xpoints
int [] ypoints
int npoints
Some public methods
addPoint(x, y) // ints
contains(x, y)
contains(p) // Point object
getBounds() // bounding rectangle
translate(dx, dy)
Details: Fig. 8.6
Revise the Rocket example using Polygon objects
One class and two interfaces
MouseEventMouseListenerMouseMotionListenerMethods 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!
Methods:
mouseMoved(event) // mouse moves
mouseDragged(event) // mouse moves with button down
MouseEvent argumentMouseEvent method
getPoint() // returns a Point
x and yKeyEvent classKeyListener interfaceMethods (Fig. 8.8):
keyTyped: high levelkeyPressed: low levelkeyReleased: low levelKeyEvent argumentJava method
setFocusable(true|false)
Displays one of four images depending on direction
new ImageIcon(filename) // create image object
icon.paintIcon(observer, graphics, x, y)
// observer can be a JPanel