How can I pass the the last added element to the ArrayList and the i element in the ArrayList to the haversineDistance
method and how can I get the lat1, lat2, long1 and long2 from them in the haversineDistance
method?
I want to iterate through the list to figure out whether the sender of the request is located close to one element in the list.
Test class:
@Path("/test")
public class Test {
private static ArrayList<LatLong> latLongList = new ArrayList<>();
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response storeData(Data data) {
String macD = data.getMac();
int routeD = data.getRoute();
float latD = data.getLatitude();
float longD = data.getLongitude();
// Add the lat and Long to the ArrayList.
latLongList.add(new LatLong(latD, longD));
int size = latLongList.size();
if (size > 1) {
for (int i = 0; i < latLongList.size(); i++) {
double distance = haversineDistance(latLongList.get(i),
latLongList.get(latLongList.size() - 1));
if (distance > 4) {
processData(macD, routeD, latD, longD);
} else {
return null;
}
}
} else {
processData(macD, routeD, latD, longD);
}
return Response.status(201).build();
}
private void processData(String macD, int routeD, float latD, float longD) {
Database db = new Database();
db.insertData(macD, routeD, latD, longD);
}
private double haversineDistance(ArrayList<LatLong> x, ArrayList<LatLong> y) {
return 0;
}
}
If you want your method haversineDistance
to compute the distance between two points , you should design the method to accept two points , not two ArrayList
of points . As a point is represented by a LatLong
object, you should change the method to:
private double haversineDistance(LatLong x, LatLong y) {
double xLat = x.getLatitude();
double xLng = x.getLongitude();
double yLat = y.getLatitude();
double yLng = y.getLongitude();
// compute haversine distance
double d = ...;
return d;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.