Q:

Ambiguous counter values assignment during a for loop after sorting a collection

I'm importing Jpeg images and adding them to a JTable.   The order is important and determined by timestamps embedded into EXIF metadata. 
I successfully organise the metadata in ascending order but when attempting to add an identifier in the form of an integer to each image I get ambiguous results.   An image at the top of the JTable should start at 1 and progressively increase. 
A System.out.println(Arrays.asList(imageList)); produces the following.   I've highlighted the image id's in bold.   

[[Jpeg{imgId=0, imgTimestamp=Sun Jan 01 01:37:30 GMT 2012,
  imgFilename=bath_pose.jpg, imgLatitude=50.4195,
  imgLongitude=-5.089666666666667}, Jpeg{imgId=0, imgTimestamp=Sat Jul
  06 14:53:11 BST 2013, imgFilename=anOxfordPhoto.jpg,
  imgLatitude=51.752833333333335, imgLongitude=-1.2536666666666667},
  Jpeg{imgId=2, imgTimestamp=Fri Aug 23 17:29:01 BST 2013,
  imgFilename=pizza.jpg, imgLatitude=10.606833333333332,
  imgLongitude=103.526}]]

I would expect to see ordering as ... imgId=0 .... imgId=1 ... imgId=2 ... with the following:
 @Override
    protected Object doInBackground() throws Exception {
        setMessage("Extracting image metadata."); 

        for (File file : selectedImages) {
            com.drew.metadata. Metadata metadata = ImageMetadataReader.readMetadata(file);

            // obtain the Exif directory
            ExifSubIFDDirectory directory = metadata.getDirectory(ExifSubIFDDirectory.class);
            GpsDirectory gpsDirectory = metadata.getDirectory(GpsDirectory.class);

            image = new Jpeg();
            image.setImgTimestamp(directory.getDate(ExifSubIFDDirectory. TAG_DATETIME_ORIGINAL));
            image.setImgFilename(file.getName());
            image.setImgLatitude(gpsDirectory.getGeoLocation().getLatitude());
            image.setImgLongitude(gpsDirectory.getGeoLocation().getLongitude());
            imageList.add(image);
        }

        Collections.sort(imageList, image);

        for (int i = 0; i < imageList.size(); i++) {
            image.setImgId(i);
        }
        System.out.println(Arrays.asList(imageList));

        return null;
    }

Jpeg Object
@XmlRootElement
public class Jpeg implements Comparator<Jpeg> {

private int imgId;
private Date imgTimestamp;
private String imgFilename;
private Double imgLatitude;
private Double imgLongitude;

public Jpeg() {
}

@XmlAttribute
public void setImgId(int imgId) {
    this.imgId = imgId;
}

@XmlElement
public void setImgTimestamp(Date imgTimestamp) {
    this.imgTimestamp = imgTimestamp;
}

@XmlElement
public void setImgFilename(String imgFilename) {
    this.imgFilename = imgFilename;
}

@XmlElement
public void setImgLatitude(Double imgLatitude) {
    this.imgLatitude = imgLatitude;
}

@XmlElement
public void setImgLongitude(Double imgLongitude) {
    this.imgLongitude = imgLongitude;
}

public int getImgId() {
    return imgId;
}

public Date getImgTimestamp() {
    return imgTimestamp;
}

public String getImgFilename() {
    return imgFilename;
}

public Double getImgLatitude() {
    return imgLatitude;
}

public Double getImgLongitude() {
    return imgLongitude;
}

public int compare(Jpeg img, Jpeg img1) {
    return img.getImgTimestamp().compareTo(img1.getImgTimestamp());
}

}

Any thoughts? 

A:

    for (int i = 0; i < imageList.size(); i++) {
        image.setImgId(i);
    }

image is the last image loaded. 
You probably meant:
    for (int i = 0; i < imageList.size(); i++) {
        imageList.get(i).setImgId(i);
    }

