我读取JSON并将数据存储到不同的对象中。我在名为Geometry
的类中存储了一些坐标。该类如下所示:
public class Geometry {
private Object[] coordinates;
private String type;
public Object[] getCoordinates() {
return coordinates;
}
}
如您所见,coordinates
字段是类Object
的数组。
知道coordinates
总是有2个位置(2个坐标),并且这些坐标的类型是double
,如何获得坐标的double
值?
更确切地说,对于系统显式返回两个坐标,我必须在以下方法上写些什么?
public double[] returnCoordinates() {
double[] coord;
coord[0] = //?
coord[1] = //?
return coord;
}
一种解决方案是将坐标对象的数组,甚至是集合存储在Geometry对象内。
class Coordinate {
private final int x;
private final int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Geometry {
private final Coordinate[] coordinates;
private final String type;
public Geometry(Coordinate[] coordinates, String type) {
this.coordinates == coordinates;
this.type = type;
}
public Coordinate[] getCoordinates() {
return coordinates;
}
}