尝试从前Array - Java </object>创建ArrayList <object>

时间:2012-09-30 18:37:33

标签: java arrays arraylist

为Android项目执行此操作;有问题从以前的对象数组创建一个ArrayList。

以下是代码WAS:

            MyReviewObject[] co = new MyReviewObject[reviews.size()];
            int index = 0;

            for (@SuppressWarnings("unused")
            String i : reviews) {
                co[index] = new MyReviewObject(datelist.get(index),
                        reviews.get(index), items.get(index),
                        cats.get(index));
                index++;
            }

            adapter = new MyReviewAdapter(getActivity(), co);
            setListAdapter(adapter);

以下是我现在所拥有的:

            ArrayList<MyReviewObject> co = new ArrayList<MyReviewObject>();


            for (String i : reviews) {
                co = new ArrayList<MyReviewObject>(datelist.add(i),reviews.add(i), items.add(i), cats.add(i));
            }

            adapter = new MyReviewAdapter(getActivity(), co);
            setListAdapter(adapter);

for循环中的语句我很难转换成工作吗?我知道我要两次声明ArrayList,应该去。

修改

这里是对象:

public class MyReviewObject {
    public String comment;
    public String date;
    public String items;
    public String cat;

    public MyReviewObject(String s1, String s2, String s3, String s4) {
        this.date = s1;
        this.comment = s2;
        this.items = s3;
        this.cat = s4;

    }

}

1 个答案:

答案 0 :(得分:2)

您不需要创建多个arraylists,只需要创建几个MyReviewObject

for (String i : reviews) {
    MyReviewObject review = new MyReviewObject(datelist.get(i),
                    reviews.get(i), items.get(i),
                    cats.get(i));
    co.add(review);
}

ps:假设原始代码有效。