I have my own class Event that has few variables like subject and start and end times. Then I have Day class that has these Events. However when I initialize Day it gets right Event list in constructor, then I store that list in local list and then try to return it in other method but it gives me different content for same list.
Here is the code to clarify things:
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();
}
}
when I print the list in constructor I get the right list. But when I print the list in getEvents() method it gives me only 1 event that may or may not be in that Day.
here is my Event class:
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;
}
}
and here is my calling code:
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());
}
}
}
Any idea what am I doing wrong?
You haven’t shown what’s calling your constructor, but the fact that you’re just copying the reference to the collection means that if the collection is changed afterwards, you’ll see those changes. For example:
My guess is that something similar is happening in your calling code – that’re your populating every
Daywith the sameArrayList, which you’re then changing. If you can post the calling code, we can verify that.You could take a defensive copy within your
Dayconstructor, changing this:to this:
Additionally, I would suggest that you change your
ArrayListvariables and parameters to be of typeList<Event>– in general, prefer to program to interfaces. It’s also not at all clear whatstartandendare meant to be in the event, and your code is currently using the default time zone of the system – is it meant to be?