我有一個叫做Coordinate的類,並且正在構建這些座標對象的向量。下面是類的外觀座標一樣 - 這是很簡單的:訪問存儲在一個向量中的對象的變量
class Coordinate {
public int x;
public int y;
// constructor
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
我的問題是,使一個載體持這種類的幾個實例,之後我將如何在給定的訪問協調對象的x或y值矢量的索引? 作爲一個例子:
v = new Vector<Coordinate>();
Coordinate a = new Coordinate(2, 3);
Coordinate b = new Coordinate(1, 4);
v.add(a);
v.add(b);
我怎麼會在向量的索引0訪問該對象的y值,並將其與在索引1對象的y值?謝謝!