gpt4 book ai didi

java - 对集合进行排序后,for 循环期间的计数器值分配不明确

转载 作者:行者123 更新时间:2023-12-01 23:23:46 25 4
gpt4 key购买 nike

我正在导入 Jpeg 图像并将其添加到 JTable 中。该顺序很重要,由嵌入 EXIF 元数据的时间戳决定。

我成功地按升序组织元数据,但是当尝试以整数形式向每个图像添加标识符时,我得到的结果不明确。 JTable 顶部的图像应从 1 开始并逐渐增加。

A System.out.println(Arrays.asList(imageList)); 产生以下结果。我已经用粗体突出显示了图像 ID。

[[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}]]

我希望看到的排序为... imgId=0 .... imgId=1 ... imgId=2 ...具有以下内容:

 @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 对象

@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());
}

}

有什么想法吗?

最佳答案

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

image 是最后加载的图像。

您的意思可能是:

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

关于java - 对集合进行排序后,for 循环期间的计数器值分配不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com