Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:54:32+00:00 2026-06-15T01:54:32+00:00

I have my own class Event that has few variables like subject and start

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:54:33+00:00Added an answer on June 15, 2026 at 1:54 am

    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:

    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
    

    My guess is that something similar is happening in your calling code – that’re your populating every Day with the same ArrayList, which you’re then changing. If you can post the calling code, we can verify that.

    You could take a defensive copy within your Day constructor, changing this:

    this.events = newEvents;
    

    to this:

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

    Additionally, I would suggest that you change your ArrayList variables and parameters to be of type List<Event> – in general, prefer to program to interfaces. It’s also not at all clear what start and end are meant to be in the event, and your code is currently using the default time zone of the system – is it meant to be?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defines its own enum like this: public class Test
I have a class called Flamethrower which naturally has its own ammunition that is
I have create my own NSOpenGLView class, right now the data that i want
I have own project and i would like add for this class same as
I have created my own Attached Property like this: public static class LabelExtension {
I have a class that is its own activity that basically i use to
I have a class generated from a WSDL that has a bunch of public
I have a class that has a UIView as a property. Sometimes I pass
I have the following code that I'd like to test: public class DirectoryProcessor {
First a little background: I have an Event model that has various event_type s.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.