利用长度自适应的数列来存储数值

//利用长度自适应的数列来存储数值,当数值的数量足够大时候,可以减少内存空间的浪费
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;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值