0

我有我的 ArrayList

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

然后用项目填充它。稍后,我想循环访问项目属性以获取数据,但是

Item[] itemArr = (Item[]) itemList.toArray();

这给了我错误:

 java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.raynemartin.SAAndroidAppCompetition.Item[]

如果这注定要失败,我还能如何访问项目的属性?这些项目位于 ArrayList 中,因此它们可以填充和 android ListView。现在我想对 ArrayList 进行排序,但需要访问我的 Item 类的 .getRating() 方法。

Item[] itemArr= (Item[])itemList.toArray();

    for(int i =0;i<itemArr.length;i++){
            for(int j=i;i<itemArr.length;j++){
                if(itemArr[j].getRating()>itemArr[i].getRating()){
                        Item temp = itemArr[j];
                        itemArr[j] = itemArr[i];
                        itemArr[i] = temp;
                }
            }
        }
    }

编辑 - 这是项目类代码

public class Item {

    private String title;
    private String category;
    private String downloads;
    private double rating;
    private Bitmap icon;

    public Item(String title, String category, String downloads,double rating,Bitmap icon) {
        this.title = title;
        this.category = category;
        this.downloads = downloads;
        this.icon = icon;
        this.rating = rating;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDownloads() {
        return downloads;
    }

    public void setDownloads(String downloads) {
        this.downloads = downloads;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }
}
4

3 回答 3

1

Item类实现接口Comparable并使用

Collections.sort(itemList);

对您的收藏进行排序。因此,您不必自己构造数组并对其进行排序。

于 2013-08-11T15:56:35.197 回答
1

如果您确实需要将元素作为数组访问,则最好使用

itemList.toArray(new Item[0]);

或者

itemList.toArray(new Item[itemList.size()]);

因为这将为您提供一个 Item 类型的数组,而不是一个对象数组。在网上其他地方有很多关于这个问题的讨论。

于 2013-08-11T15:59:32.727 回答
1

要排序,您可以尝试以下方法。

在您的 Item 类中实现 toString

public String toString()
{
    return String.valueOf(rating);
}

实现一个比较器

 class SortingRating implements Comparator<Item>
{

        @Override
        public int compare(Item arg0, Item arg1) {
            if (arg0.getRating() < arg1.getRating()) return -1;
            if (arg0.getRating() > arg1.getRating()) return 1;
            return 0;
    }

}

然后对您的列表进行排序

   Collections.sort(itemList, new SortingRating());

示例:如果我错了,请纠正我。

public class MainClass {
    static ArrayList<Item> aa = new ArrayList<Item>();
    public static void main(String[] args) {

        Item i1 = new Item("soap","sales","yes",4.0);
        aa.add(i1);
        Item i2 = new Item("soap","sales","yes",5.2);
        aa.add(i2);
        Item i3 = new Item("soap","sales","yes",3.8);
        aa.add(i3);
        Item i4 = new Item("soap","sales","yes",5.1);
        aa.add(i4);

         Collections.sort(aa, new SortingRating());
         for(int i=0;i<aa.size();i++)
         {
         System.out.println("Title  : " +aa.get(i).getTitle()+"--"+"Category : "+aa.get(i).getCategory()+"--"+"Downlaods : "+aa.get(i).getDownloads()+"--"+"Rating : "+aa.get(i).getRating());
         }

    }
}
class SortingRating implements Comparator<Item>
{

        @Override
        public int compare(Item arg0, Item arg1) {
            if (arg0.getRating() < arg1.getRating()) return -1;
            if (arg0.getRating() > arg1.getRating()) return 1;
            return 0;
        }   
}

物品类。遗漏位图图标

public class Item  {

    private String title;
    private String category;
    private String downloads;
    private double rating;


    public Item(String title, String category, String downloads,double rating) {
        this.title = title;
        this.category = category;
        this.downloads = downloads;
        this.rating = rating;
    }
    public String toString()
    {
        return String.valueOf(rating);
    }
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDownloads() {
        return downloads;
    }

    public void setDownloads(String downloads) {
        this.downloads = downloads;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }
}

输出

Title  : soap--Category : sales--Downlaods : yes--Rating : 3.8
Title  : soap--Category : sales--Downlaods : yes--Rating : 4.0
Title  : soap--Category : sales--Downlaods : yes--Rating : 5.1
Title  : soap--Category : sales--Downlaods : yes--Rating : 5.2
于 2013-08-11T16:22:37.223 回答