构造函数和方法中数组的内容不同

时间:2012-11-27 07:23:45

标签: java list constructor arraylist

我有自己的班级Event,其中包含主题开始以及结束次数等变量。然后我有Day类,其中包含EventsDay。但是,当我初始化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()

当我在构造函数中打印列表时,我得到了正确的列表。但是,当我使用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; } } 中的事件。

这是我的Event类:

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

这是我的主叫代码:

{{1}}

知道我做错了什么吗?

2 个答案:

答案 0 :(得分: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在事件中应该是什么,并且您的代码当前正在使用系统的默认时区 - 它是否意味着?

答案 1 :(得分:0)

嗨,没有什么不对,我用字符串arraylist测试它,它正常工作。

但是我们不知道你如何填充你的arraylist并将它传递给你的班级,如果它如下,它应该正常工作。

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