gpt4 book ai didi

android - Android 中的 ArrayList 排序问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:04:20 25 4
gpt4 key购买 nike

我有一个对象的 ArrayList。

ArrayList<Item> blog_titles = new ArrayList<Item>();

我想按照其中一个数据成员的降序对 ArrayList 进行排序,该数据成员是一个存储为字符串的 DateTime 值(下面代码中的timestamp)。

public class BlogItem implements Item, Comparable<BlogItem> {

public final String id;
public final String heading;
public final String summary;
public final String description;
public final String thumbnail;
public final String timestamp; // format:- 2013-02-05T13:18:56-06:00
public final String blog_link;

public BlogItem(String id, String heading, String summary, String description, String thumbnail, String timestamp, String blog_link) {
this.id = id;
this.heading = heading;
this.summary = summary;
this.description = description;
this.thumbnail = thumbnail;
this.timestamp = timestamp; // format:- 2013-02-05T13:18:56-06:00
this.blog_link = blog_link;
}

@Override
public int compareTo(BlogItem o) {
// TODO Auto-generated method stub
return this.timestamp.compareTo(o.timestamp);
}

}

Item 是一个通用接口(interface):

public interface Item { 
// TODO Auto-generated method stub
}

现在,当我尝试对 ArrayList 进行排序时:

Collections.sort(blog_titles);

我收到以下错误消息:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Item>). The inferred type Item is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

我该如何解决上述错误?在这种情况下,这是对 ArrayList 进行排序的正确方法吗?

最佳答案

你的 blog_titles列表是 Item 的列表.

Item本身不是Comparable , 而 BlogItem是。

要么声明blog_titles作为 ArrayList<BlogItem> , 或制作 Item扩展 Comparable

关于android - Android 中的 ArrayList 排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16563845/

25 4 0