为什么ArrayList <object>被最后添加的对象覆盖?

时间:2016-07-29 18:30:15

标签: java list object arraylist

当我实现此代码时,它会打印出最后一个值:

public static void createData(File f) {

    List<LatLonData> point = new ArrayList<>();
    int j=0;
    LatLonData tempObj;

    try {                        

        BufferedReader br = new BufferedReader(new FileReader(f));

        String fileRead = br.readLine();

        while(fileRead != null) {

            // split file by commas
            String[] token = fileRead.split(",");

            double tempLat = Double.parseDouble(token[0]);
            double tempLon = Double.parseDouble(token[1]); 
            double tempSpeed =  Double.parseDouble(token[2]);                 

            tempObj = new LatLonData(tempLat,tempLon,tempSpeed);
            point.add(tempObj);

            // Tests to see if tempObj are getting correct data 
            // and point are getting correct data
            System.out.println(tempObj);
            System.out.println(point.get(j).toString());
            j++;


            fileRead = br.readLine();
            }
            // close file
            br.close();
        }
        catch (FileNotFoundException fnfe) {
            System.out.println("File not found");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        for(LatLonData l: point) {
            System.out.println(l);
        }

}

当我在while循环中测试这段代码时,如果发现tempObj和point对象都在那里得到正确的数据。但是当我在while循环之外打印数据时,整个数据集都会填充最后一个对象。似乎每个都经历过,所有的旧物体都被删除了。以下是打印输出的简短示例:

  lon: 113.5041  lat: 82.1708  speed: 0.56  
  lon: 113.5041  lat: 82.1708  speed: 0.56  
  lon: 113.5042  lat: 82.1708  speed: 1.07  
  lon: 113.5042  lat: 82.1708  speed: 1.07  
  lon: 113.5041  lat: 82.1708  speed: 1.0  
  lon: 113.5041  lat: 82.1708  speed: 1.0  
  lon: 113.5041  lat: 82.1707  speed: 0.94  
  lon: 113.5041  lat: 82.1707  speed: 0.94  
  lon: 113.5041  lat: 82.1707  speed: 1.18  
  lon: 113.5041  lat: 82.1707  speed: 1.18  

  lon: 113.504  lat: 82.1707  speed: 1.35  
  lon: 113.504  lat: 82.1707  speed: 1.35  
  lon: 113.504  lat: 82.1707  speed: 1.35  
  lon: 113.504  lat: 82.1707  speed: 1.35  
  lon: 113.504  lat: 82.1707  speed: 1.35  
  lon: 113.504  lat: 82.1707  speed: 1.35  

这是我的LatLonData文件以防万一:

public class LatLonData {

    private static double lonXY;
    private static double latXY;
    private static double speedMPH;

    public LatLonData() {

    }

    public LatLonData(double lonXY, double latXY, double speedMPH) {

        this.lonXY = lonXY;
        this.latXY = latXY;
        this.speedMPH = speedMPH;

    }

    public static double getLat() {
        return latXY;
    }

    public static double getLon() {
        return lonXY;
    }

    public static double getSpeed() {
        return speedMPH;
    }

    // setters
    public void setLat(double latXY) {
        this.latXY = latXY;
    }

    public void setLon(double lonXY) {
        this.lonXY = lonXY;
    }

    public void setSpeed(double speedMPH) {
        this.speedMPH = speedMPH;
    }

    public String toString() {

        return "  lon: " + lonXY + "  lat: " + latXY + "  speed: " + speedMPH;
    }   
}

0 个答案:

没有答案