向 ArrayList 添加元素

add elements to the ArrayList

我试图将请求的地理坐标存储在 ArrayList 中,以检查它们中的一些是否位于同一个外接圆中,但在每次请求后只是将一个元素添加到列表中!我如何管理它以在每次调用请求后将所有元素添加到 ArrayList?

接收者class:

    import java.util.ArrayList;
    import java.util.List;

    import javax.ws.rs.Consumes;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;


@Path("/test")
public class Receiver  {

    private ArrayList<LatLong> latLongList;
    @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();

        latLongList = new ArrayList<>();

        latLongList.add(new LatLong(latD, longD));

        System.out.println("latLondList size: " + latLongList.size());


        Database db = new Database();
        db.insertData(macD, routeD, latD, longD);

        return Response.status(201).build();
    }
}

数据class:

@Provider
public class Data {
    private String mac;
    private int route;
    private float latitude;
    private float longitude;

  //getter setter constructor.
}

经纬度class:

public class LatLong {

    float latitude;
    float longitude;

    //getter setter constructor.
}

试试这个:

public class Receiver  {
    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();

        latLongList.add(new LatLong(latD, longD));

        System.out.println("latLondList size: " + latLongList.size());


        Database db = new Database();
        db.insertData(macD, routeD, latD, longD);

        return Response.status(201).build();
    }
}

编辑: 删除了第二个 latLongList 变量。