//利用长度自适应的数列来存储数值,当数值的数量足够大时候,可以减少内存空间的浪费
public class StackOfPoints {
private double x[];
private double y[];
private int size;
private final int capacity = 8;
public StackOfPoints() {
x = new double[8];
y = new double[8];
}
//create an object with a specified number of pairs of points
public StackOfPoints(int newCapacity) {
x= new double [ newCapacity ];
y = new double [ newCapacity ];
}
public void push(double newX, double newY) {
if ( size >= x.length) {
double tempX[] = new double[x.length+1];
double tempY[] = new double[y.length+1];
System.arraycopy(x, 0, tempX, 0, x.length);
System.arraycopy(y, 0, tempY, 0, y.length);
x = tempX;
y = tempY;
}
x[size] = newX;
y[size] = newY;
size ++ ;
}
//返回位于数列最前面坐标
public double[] pop() {
size -- ;
double[] temp = new double[2];
temp[0] = x[size];
temp[1] = y[size];
return temp;
}
public double[] peek() {
double[] temp = new double[2];
temp[0] = x[size-1];
temp[1] = y[size-1];
return temp;
}
}