0

I have created timeslots for patient scheduling. I have made 160 of them for one week but I want thousand weeks, so for every timeslot I have made a variable week (int). Now for some reason, every timeslot gets the value 1000 for the variable week. If I test the same code on a test variable it works just fine. Anyone has an idea?

    int[] test = new int[160000];
    for(int j =0;j<1000;j++)
    {
      for(int i = 0;i < 160;i++)
     {
         timeslot[j*160 + i] = timeslot_build[i];
         timeslot[j*160 + i].set_week(j+1);
         test[j*160 + i] = (j+1);

     }      

    }


     System.out.println(test[150]);
     System.out.println(test[5166]);
     System.out.println(test[44000]);
     System.out.println(test[100000]); 

     System.out.println(timeslot[150].week);
     System.out.println(timeslot[5166].week);
     System.out.println(timeslot[44000].week);
     System.out.println(timeslot[100000].week); 

This is the output:

1 33 276 626 1000 1000 1000 1000

5
  • How do you initialize test? Commented Apr 26, 2019 at 10:06
  • Could you provide timeslot, or check that timeslot.set_week does set the week field properly ? Commented Apr 26, 2019 at 10:06
  • What is timeslot_build? Why do you assign twice the week value to timeslot(.set_week and .week)? Commented Apr 26, 2019 at 10:07
  • Assigning this value twice is a mistake but it shouldn't matter. Commented Apr 26, 2019 at 10:16
  • Timeslot_build were the initial timeslots for only one week Commented Apr 26, 2019 at 10:20

1 Answer 1

2

When you assign timeslot[j*160 + i] = timeslot_build[i] you use same instance.

So timeslot[0*160 + i] = timeslot[1*160 + i] = timeslot[2*160 + i] etc.

You have to create new instance every time. Because now you have only 160 of whatever type timeslot have and timeslot array store references for them.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm sorry, can you clarify that. This is how I initialized timeslots: Timeslot[] timeslot = new Timeslot[160000];
How you initialize timeslot_build?
Ah, I understand what you mean but that's actually what I want. I want timeslot[0*160 + i] = timeslot[1*160 + i] = timeslot[2*160 + i], only the weeks should differ.
You can't have one instance have different values in it field.
timeslot[0*160 + i] = timeslot[1*160 + i] mean they refer to single instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.