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

  • Home
  • SEARCH
  • 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 4060886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:23:46+00:00 2026-05-20T15:23:46+00:00

I have a problem with getting a new value of an object. I have

  • 0

I have a problem with getting a new value of an object. I have a code like that:

...
TimeSchedule[] offspringScheduleOne = new TimeSchedule[AVAILABLE_CLASSROOMS];
...
offspringScheduleOne[i] = genes.get(geneOneIndex).getSchedule()[i];
...

After that assignment offspringScheduleOne[i] and genes.get(geneOneIndex).getSchedule()[i] points the same memory address. I want that: offspringScheduleOne[i] should get the value of the genes.get(geneOneIndex).getSchedule()[i], they musn’t be same, they just should have same values.

TimeSchedule class:

public class TimeSchedule extends AlgorithmParameters {
    public int[][] timetable = new int[DAYS][HOURS];//DAYS and HOURS are static final variables that comes from AlgorithmParameters 

    public int[][] getTimetable() {
        return timetable;
    }

    public void setTimetable(int[][] timetable) {
        this.timetable = timetable;
    }
}

How can I do that?

  • 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-05-20T15:23:47+00:00Added an answer on May 20, 2026 at 3:23 pm

    It actually is copying the value – but you need to understand what that value is.

    The value of offspringScheduleOne[0] isn’t a TimeSchedule object. It’s a reference to a TimeSchedule object. No expression in Java has a value which is an object. It’s really important that you understand this.

    Now, if you want a copy of the object, you’ll have to make that happen yourself. For example, you could include a clone() method in TimeSchedule, and write:

    offspringScheduleOne[i] = genes.get(geneOneIndex).getSchedule()[i].clone();
    

    In other words, create a clone of the existing object, and then set offspringScheduleOne[i] to be a reference to that newly created object. Of course, if any of the fields within TimeSchedule is a reference type field, you’ll need to consider whether or not you need to clone that object as well…

    … or you could add a constructor and call that, or another method, etc. But you need to be absolutely clear that the assignment operator is copying the value, but that value is a reference.

    EDIT: Okay, now that you’ve posted TimeSchedule, a few suggestions:

    • Stop using public fields. What’s the point of having properties if the field is public?
    • Rather than having properties returning the whole array, change them to access an individual hour, e.g.

      public int getTimetable(int day, int hour) {
          // TBD: Argument validation
          return timetable[day][hour];
      }
      
      // Similar for `setTimetable`
      
    • Create a clone method like this:

      public TimeSchedule clone() {
          TimeSchedule copy = new TimeSchedule();
          for (int i = 0; i < timetable.length; i++) {
              copy.timetable[i] = timetable[i].clone();
          }
          return copy;
      }
      

      (That’s slightly wasteful in that it will create the subarrays and then discard them, but let’s get something which works first…)
      }

      public Test clone() {
      int[][] timetableCopy = new int[timetable.length][];
      for (int i = 0; i < timetable.length; i++) {
      timetableCopy[i] = timetable[i].clone();
      }
      return null;
      }

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

Sidebar

Related Questions

I have a problem getting my change(s) to data object retrieved using NHibernate to
I have a problem regarding getting the path of a user control. The scenario
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I do not have problem as such but I am quite new to Ruby.
I have class foo that contains a std::auto_ptr member that I would like to
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem when I try insert some data to Informix TEXT column via

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.