如果你坚持使用二维数组,你可以像这样映射你的值:
array[0][0] = longitude[0];
array[0][1] = latitude[0];
array[0][2] = altitude[0];
array[1][0] = longitude[1];
array[1][1] = latitude[1];
array[1][2] = altitude[1];
...
array[n][0] = longitude[n];
array[n][1] = latitude[n];
array[n][2] = altitude[n];
更好的解决方案是创建一个Position 类来保存一个经度、纬度和高度。然后你可以拥有一个由Position 实例组成的一维数组。
编辑添加:
这是一个简单的例子。我用你的四个位置线作为输入。
-179.75,-89.75,-1965
-179.75,-89.5,-2011
-179.75,-89.25,-2140
-179.75,-89,-2162
我收到以下经度 -179.75,纬度 -89.5 的输出。
-2011
我在创建的一维数组中使用了简单的线性搜索。对于 4 个值,它足够快。对于 65,000 个值,您可以运行我的代码并查看需要多长时间。正如我所说,如果您按经度、纬度对数组进行排序并使用二进制搜索,您将在大约 32 次测试中得到正确的结果。
这是我使用的完整可运行代码。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PositionApp {
public static void main(String[] args) {
new PositionApp().run();
}
private Position[] positions;
public void run() {
try {
int count = readCSVFile();
this.positions = new Position[count];
processCSVFile();
int altitude = getAltitude(-179.75, -89.5);
System.out.println(altitude);
} catch (IOException e) {
e.printStackTrace();
}
}
public int readCSVFile() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("/sample.csv")));
int count = 0;
String line = reader.readLine();
while (line != null) {
count++;
line = reader.readLine();
}
reader.close();
return count;
}
public void processCSVFile() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("/sample.csv")));
int count = 0;
String line = reader.readLine();
while (line != null) {
String[] parts = line.split(",");
double longitude = Double.valueOf(parts[0]);
double latitude = Double.valueOf(parts[1]);
int altitude = Integer.valueOf(parts[2]);
positions[count++] = new Position(longitude, latitude, altitude);
line = reader.readLine();
}
reader.close();
}
public int getAltitude(double longitude, double latitude) {
for (int index = 0; index < positions.length; index++) {
if ((positions[index].getLongitude() == longitude) &&
(positions[index].getLatitude() == latitude)) {
return positions[index].getAltitude();
}
}
return Integer.MIN_VALUE;
}
public class Position {
private final int altitude;
private final double longitude;
private final double latitude;
public Position(double longitude, double latitude, int altitude) {
this.longitude = longitude;
this.latitude = latitude;
this.altitude = altitude;
}
public int getAltitude() {
return altitude;
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
}
}