-1

So here's what I have right now:

public static void main(String[] args) {
    Calendar[][] times = new Calendar[10][2];
    Calendar begin = Calendar.getInstance();
    for(int i = 0; i < 10; i++) {
        times[i][0] = begin;
        begin.add(Calendar.MINUTE, 120);
        times[i][1] = begin;
    }
    for(int i = 0; i < 10; i++) {
        System.out.println("Start time:"  + times[i][0].getTime() + " and end time is: " + times[i][1].getTime());
    }

}

To my understanding of how add works in the Calendar class, I would expect that times[0][0] would be time of compilation, times [0][1] would be 120 minutes in the future. Next run through the loop times[1][0] would equal times[0][1] and times[1][1] would be 120 minutes ahead of that. Is that incorrect? It currently outputs the same time for each member of the array.

EDIT: Thanks for the help everyone, here's the correct code:

public static void main(String[] args) {
    Calendar[][] times = new Calendar[10][2];
    Calendar begin = Calendar.getInstance();
    for(int i = 0; i < 10; i++) {
        times[i][0] = (Calendar) begin.clone();
        begin.add(Calendar.MINUTE, 120);
        times[i][1] = (Calendar) begin.clone();
    }
    for(int i = 0; i < 10; i++) {
        System.out.println("Start time:"  + times[i][0].getTime() + " and end time is: " + times[i][1].getTime());
    }

}
  • 2
    Object Arrays store references to objects, not objects themselves. `for(int i=0; i < 20; i++) {arr[i] = foo;}`, this stores 20 references to the same object. A change to `foo` in any of the 20 cells changes all 20 cells. – Compass May 07 '18 at 20:17
  • 1
    Why would you clone the entire `Calendar` object, when all you want is the current date/time value it has calculated for you? Change `times` to a `Date[][]` and change code to e.g. `times[i][0] = begin.getTime();` --- Better yet, stop using the legacy `Calendar` class, and use Java 8's `ZonedDateTime` instead. It is immutable, so the issue you experienced couldn't even happen. – Andreas May 07 '18 at 20:32
  • Any particular reason why you are still struggling with the long outdated `Calendar` class? I recommend you use [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) instead. It is so much nicer to work with. – Ole V.V. May 08 '18 at 01:44

2 Answers2

0

You assign references of begin to all objects in times array. What you do in fact is changing the same value of begin - which is referenced from all array elements.

If you need a new calendar instance somehow relative to existing one, you might want to clone it:

times[i][0] = (Calendar) begin.clone();
Calendar hourLater = (Calendar) begin.clone();
hourLater(Calendar.MINUTE, 120);
times[i][1] = hourLater;
begin = hourLater;
  • So how would I fix that? Should I be used set instead of add? – itsgreylolol May 07 '18 at 20:07
  • You need to clone the object and assign the clone of it to the array. – Ivar May 07 '18 at 20:10
  • I'm not sure I understand, won't the clone still be assigned references? I need the time to update after each add statement, and I can't do something like temp = begin.add() because add returns void. – itsgreylolol May 07 '18 at 20:15
  • OK, I see the code for cloning the Calendar, but how do I add time to it? The add function no longer functions on a generic object – itsgreylolol May 07 '18 at 20:17
  • @itsgreylolol No. When you create a `Calendar` object it is somewhere in the memory, and the `begin` just points to that location. When you assign `begin` to your array it just adds the reference to that location, but they still are pointing to the same location. Cloning the object actually creates a new instance of the object in the memory. If you change that one, the other one wont change. – Ivar May 07 '18 at 20:18
  • Got it, updating post with correct code. Thank you guys – itsgreylolol May 07 '18 at 20:19
0
import java.util.*;
public class HelloWorld{

     public static void main(String []args){
            Calendar[][] times = new Calendar[10][2];
            Calendar begin = Calendar.getInstance();
            for(int i = 0; i < 10; i++) {
                times[i][0] = begin;
                begin = (Calendar)begin.clone();
                begin.add(Calendar.MINUTE, 120);
                times[i][1] = begin;
            }
            for(int i = 0; i < 10; i++) {
                System.out.println("Start time:"  + times[i][0].getTime() + " and end time is: " + times[i][1].getTime());
            }

     }
}
Eugen
  • 877
  • 6
  • 16