我有自己的课程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());
}
}
}
知道我在做什么错吗?