1

我有自己的课程Event,其中很少有变量,例如主题开始结束时间。然后我Day上课有这些Events。但是,当我初始化Day它时,它会在构造函数中获得正确的Event列表,然后我将该列表存储在本地列表中,然后尝试以其他方法返回它,但它为同一列表提供了不同的内容。

这是澄清事情的代码:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Day {

    private String date;
    private ArrayList<Event> events = new ArrayList<Event>();
    private SimpleDateFormat dayDotMonth = new SimpleDateFormat("dd.MM EEEE");

    public Day(int date, ArrayList<Event> newEvents){
        this.events = newEvents;

        System.out.println("FROM Constructor:");
        for (Event event : this.events) {
            System.out.println(event.getSubject()); // CORRECT LIST
        }

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date*1000L);
        this.date = dayDotMonth.format(cal.getTime());

    }

    public String getDate(){
        return this.date;
    }

    public ArrayList<Event> getEvents(){

        System.out.println("FROM getEvents():");
        for (Event event : this.events) {
            System.out.println(event.getSubject()); // INCORRECT LIST
        }
        return this.events;
    }

    public int getAmountOfEvents(){
        return this.events.size();
    }
}

当我在构造函数中打印列表时,我得到了正确的列表。但是,当我在方法中打印列表时,getEvents()它只给了我 1 个事件,它可能存在也可能不存在Day

这是我的事件类:

    public class Event {

        private int start, end;
    private String subject, eventId, description;

    public Event(int start, int end, String subject, String eventId, String description) {
        this.start = start;
        this.end = end;
        this.subject = subject;
        this.description = description;
        this.eventId = eventId;

    }

    public int getStart() {
        return this.start;
    }

    public int getEnd() {
        return this.end;
    }

    public String getSubject() {
        return this.subject;
    }

    public String getEventId() {
        return this.eventId;
    }

    public String getDescription() {
        return this.description;
    }
}

这是我的调用代码:

 private void getObjects(String url) throws JSONException, Exception {
            JSONObject jsonObject = new JSONObject(new NetTask().execute(url).get());
            JSONArray job1 = jsonObject.getJSONArray("events");
            ArrayList<Event> events = new ArrayList<Event>();
            Calendar calPrev = Calendar.getInstance();
            int prevDate = 0;
            boolean first = true;

            for (int i = 0; i < job1.length(); i++) {
                JSONObject myJsonObject = job1.getJSONObject(i);
                int start = myJsonObject.getInt("start");
                int end = myJsonObject.getInt("end");
                String subject = myJsonObject.getString("subject");
                String eventId = myJsonObject.getString("eventid");
                String description = myJsonObject.getString("description");

                if(first){
                    prevDate = start;
                    calPrev.setTimeInMillis(start*1000L);
                    events.add(new Event(start,end,subject,eventId,description));
                    first = false;
                }else{
                    Calendar calCur = Calendar.getInstance();
                    calCur.setTimeInMillis(start*1000L);

                    if(calPrev.get(Calendar.YEAR) == calCur.get(Calendar.YEAR) && calPrev.get(Calendar.DAY_OF_YEAR) == calCur.get(Calendar.DAY_OF_YEAR)){
                        events.add(new Event(start,end,subject,eventId,description));
                        calPrev.setTimeInMillis(start*1000L);
                    }else{
                        calPrev.setTimeInMillis(start*1000L);
                        this.days.add(new Day(prevDate,events));
                        prevDate = start;
                        events.clear();
                        events.add(new Event(start,end,subject,eventId,description));
                    }
                }
            }
            this.days.add(new Day(prevDate,events));

System.out.println("Last day added to list\nPrinting events from days:");
        for (Day day : this.days){
            ArrayList<Event> events = day.getEvents();
            for(Event event : events){
            System.out.println(event.getSubject());
            }
        }
        }

知道我在做什么错吗?

4

2 回答 2

2

您尚未显示调用构造函数的内容,但您只是复制对集合的引用这一事实意味着,如果集合随后发生更改,您将看到这些更改。例如:

ArrayList<Event> events = new ArrayList<Event>();
events.add(new Event(0, 1, "id", "subject", "description"));
Day day = new Day(0, events);
events.clear();
System.out.println(day.getEvents().size()); // 0

我的猜测是在您的调用代码中发生了类似的事情 - 那是您Day使用相同的填充每个ArrayList,然后您正在更改。如果您可以发布调用代码,我们可以验证。

您可以在Day构造函数中获取防御性副本,更改以下内容:

this.events = newEvents;

对此:

this.events = new ArrayList<Event>(newEvents);

此外,我建议您将ArrayList变量和参数更改为类型List<Event>- 通常,更喜欢对接口进行编程。也完全不清楚事件中的内容startend含义,并且您的代码当前正在使用系统的默认时区 - 它是这样的吗?

于 2012-11-27T07:37:54.563 回答
0

您好,没有错,我用字符串数组列表测试了它,它工作正常。

但是我们不知道您如何填充您的数组列表并将其传递给您的班级,如果如下所示,它应该可以正常工作。

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

        ArrayList<String> arr = new ArrayList<String>();
        arr.add("1");
        arr.add("2");
        arr.add("3");
        arr.add("4");
        arr.add("5");
        arr.add("6");

        Day d = new Day(1, arr);
        d.getStrings();

    }


package com.talha.examples;    
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Day {

    private String date;
    private ArrayList<String> Strings = new ArrayList<String>();
    private SimpleDateFormat dayDotMonth = new SimpleDateFormat("dd.MM EEEE");

    public Day(int date, ArrayList<String> newStrings){
        this.Strings = newStrings;

        System.out.println("FROM Constructor:");
        for (String s : this.Strings) {
            System.out.println(s); // CORRECT LIST
        }

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date*1000L);
        this.date = dayDotMonth.format(cal.getTime());

    }

    public String getDate(){
        return this.date;
    }

    public ArrayList<String> getStrings(){

        System.out.println("FROM getStrings():");
        for (String s : this.Strings) {
            System.out.println(s); // INCORRECT LIST
        }
        return this.Strings;
    }

    public int getAmountOfStrings(){
        return this.Strings.size();
    }
}
于 2012-11-27T07:37:22.807 回答