我正在开发一个 Android 应用程序。我需要根据另一个数组的排序对数组进行排序。我正在根据从最低到最高对一个(距离)进行排序,并且需要根据距离对我的经度值进行排序。因此,假设距离 5 的经度为 41.2265,距离 6 的经度为 41.2187,我需要将距离从最低到最高排序,{5,6},然后根据它们的第一对对经度进行排序。我读过你可以用二维数组来做这件事,但我不想这样做。我认为这也可以通过映射来完成,但我不确定如何。我的代码如下:
NearestStations.java 的一部分
ArrayList<String> distancetos = new ArrayList<String>();
ArrayList<String> longitudeArray = new ArrayList<String>();
while(iterator.hasNext()){
for (int i=0; i<144;i++){
double distance = 0;
double lat_end = 0;
double lon_end = 0;
try {
lat_end = Double.parseDouble(iterator.next());
lon_end = Double.parseDouble(iterator1.next());
longitudeArray.add(Double.toString(lon_end));
Log.i("Lon_end", String.valueOf(lon_end));
} catch (NumberFormatException e) {
Log.v("Main", "Convert to Double Failed : ");
}
Location locationA = new Location("point A");
locationA.setLatitude(latitude);
locationA.setLongitude(longitude);
Location locationB = new Location("point B");
locationB.setLatitude(lat_end);
locationB.setLongitude(lon_end);
distance = locationA.distanceTo(locationB) * 0.000621371192237334;
Log.i("distancebefore", String.valueOf(distance));
String dista = Double.toString(distance);
distancetos.add(dista);
}
}
Collections.sort(distancetos);
distancea = distancetos.get(0);
distance1 = distancetos.get(1);
String Longa = longitudeArray.get(0);
String Long1 = longitudeArray.get(1);
Log.i("distanceafter", String.valueOf(distancea));
Log.i("distance1after", String.valueOf(distance1));
String[] Stations = getResources().getStringArray(R.array.Stations);
String[] Longitude = getResources().getStringArray(R.array.Longitude);
String[] Latitude = getResources().getStringArray(R.array.Latitude);
Map<String, String> myMap = new HashMap<String, String>();{
for (int i = 0; i <144; i++) {
myMap.put(Latitude[i], Stations[i]);
}
}
Map<String, String> myMap1 = new HashMap<String, String>();{
for (int h = 0; h <144; h++) {
myMap1.put(Longitude[h], Stations[h]);
}
}
String value = myMap1.get(Longa);
}
}
谢谢您的帮助。