CSC 102 Lecture Notes Week 3
Lab and Program Discussion
Program Design
Arrays and ArrayLists
Enter integers, doubles, bools, or Strings; Enter "quit" when done: 1 2 -10 6 4 2.5 15.6 12.2 true false true hi there quit Minumum integer is: -10 Average double is: 10.1 Number of trues is: 2 Number of Strings is: 2
Enter integers, doubles, bools, or Strings; Enter "quit" when done:
quit
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Filter.minimumInt(Filter.java:35)
at Lab5Driver.main(Lab5Driver.java:44)
Now on to Arrays and ArrayLists, in Horstmann Chapter 7
ArrayList<Integer> al = new ArrayList<Integer>();
instead ofal.get(i);
al[i]
instead ofal.set(i, 10);
al[i] = 10;
for (int i : al) { ... }
import java.util.Arrays;
import java.util.ArrayList;
/****
*
* This class illustrates some of the basic ideas for arrays and ArrayLists.
* In 102, you'll primarily be using ArrayLists instead of arrays, but use of
* arrays may be convenient in some cases. NOTE: In labs and programming
* assignments where it says you must use an ArrayList, using an plain array
* will not do.
*/
public class ArraysAndArrayLists {
public static void main(String[] args) {
// Allocate a 10-element array.
int a[] = new int[10];
// Allocate a flexible-size ArrayList.
ArrayList<Integer> al = new ArrayList<Integer>();
// Assign the values 0 through 90 to both the array and ArrayList.
for (int i = 0; i < 10; i++) {
a[i] = i * 10;
al.add(i * 10);
}
// Print out the elements of the array, using a standard for loop.
for (int i = 0; i < 10; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
// Print out the elements of the ArrayList, using standard for loop.
for (int i = 0; i < 10; i++) {
System.out.print(al.get(i) + " ");
}
System.out.println();
// Increment each element of the array and ArrayList by 1.
for (int i = 0; i < 10; i++) {
a[i]++;
al.set(i, al.get(i) + 1);
}
//
// Print out the elements of the array and ArrayList in different ways.
//
// Use the Arrays.toString library method on the array.
System.out.println(Arrays.toString(a));
// Use (indirectly) the ArrayList.toString method.
System.out.println(al);
// Use the specialized form of for loop on ArrayLists.
for (int i : al) {
System.out.print(i + " ");
}
System.out.println();
// Try to print the array directly; what's going on here?
System.out.println(a + " -- Say what?");
}
}
ArrayList<Integer> al = new ArrayList<Integer>();
ArrayList<Object> al = new ArrayList<Object>();
ArrayList<int> al;
ArrayList<Integer> = new ArrayList<Integer>
ArrayList<int> = new ArrayList<int>
al.add(10);
al.add(new Integer(10));
int i = al.get(x);
int i = al.get(x).intValue();
isdouble methodX(int i, String s, boolean b);
which reads "A method of int , String , boolean returning double".(int, String, boolean) -> double
public class Drawing {
ArrayList<Rectangle> canvas;
public static void main(...) {
// Draw some stuff on the canvas
}
}
But this is really boring, since all I can draw is rectangles.
public class Drawing {
ArrayList<Shape> canvas;
public static void main(...) {
// Draw some stuff on the canvas
}
}
public interface Shape {
public void move(Point delta);
public double getArea();
. . . // more later
}
public class Rectangle implements Shape {
int x,y,height,width;
public void move(Point delta) {
...
}
public double getArea() {
...
}
}
public class Shape {
// Leave out data fields
public void move(Point delta) {
// no default implementation
}
public double getArea() {
return 0; // pretty useless
}
. . . // maybe more later
}
public class Rectangle extends Shape {
int x,y,height,width;
public void move(Point delta) {
...
}
public double getArea() {
...
}
}
Now on to a Bit of Program Design, from Horstmann Chapter 8
/****
*
* A simple Java program that defines a rectangle data structure
* and methods that operate on rectangles.
*
*/
public class Rectangle {
int x;
int y;
int width;
int height;
Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
void move(int x_increment, int y_increment) {
x = x + x_increment;
y = y + y_increment;
}
boolean equals(Rectangle r) {
return x == r.x &&
y == r.y &&
width == r.width &&
height == r.height;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}