Search code examples
javaarraysarraylistcorruption

ArrayList.add(idx,element) corrupt other element of array


I'm running a program that:

  1. Create an ArrayList
  2. Add some element on the top of this array
  3. Create a "base" element which will be decreased
  4. Then with a for loop decrease the base element and add it on the top of the array (index 0)
  5. Unexpectedly the output that I get is terrifying

Please don't feel bad about some italian word:

  • "ora" means hour, so

    Ora hour = new Ora(6,34) //--> 6:34
    
  • "decrementaMinuti" decrease minutes from the hour

This is the full code:

public static void main(String[] args) {

    ArrayList<Ora> hours = new ArrayList<>();

    hours.add(0, new Ora(01,00));
    hours.add(0, new Ora(00,00));

    Ora base = hours.get(0);
    System.out.println("Base: " + base + "\n");        

    for (int i = 0; i < 4; i++) {

        System.out.println("First: " + base);
        // decreasing 60 minutes from hour
        base.decrementaMinuti(60);

        System.out.println("After: " + base);
        hours.add(0, base);

        System.out.println("In Array: " + hours.get(0));

        System.out.println("[hours]");
        for (int j = 0; j < hours.size(); j++) {
            System.out.println("[" + hours.get(j) + "]");
        }

        System.out.println("- - - - - - - -\n");
    }

}

And this is the output I got:

Base: 11:00

First: 11:00
After: 10:00
In Array: 10:00
[hours]
[10:00]
[10:00]
[12:00]
- - - - - - - -

First: 10:00
After: 09:00
In Array: 09:00
[hours]
[09:00]
[09:00]
[09:00]
[12:00]
- - - - - - - -

First: 09:00
After: 08:00
In Array: 08:00
[hours]
[08:00]
[08:00]
[08:00]
[08:00]
[12:00]
- - - - - - - -

First: 08:00
After: 07:00
In Array: 07:00
[hours]
[07:00]
[07:00]
[07:00]
[07:00]
[07:00]
[12:00]
- - - - - - - -

In the last block of output there are the hour 7:00 5 times when I never add the same hour two times.

I'm asking: why (as you can see) adding an element of the top of this array cause that also the after elements were corrupted?

My java version:

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Any help you can procide is really appreciated.


Solution

  • You are adding the same instance multiple times to the List :

    Ora base = hours.get(0); // this is the instance added multiple times
    System.out.println("Base: " + base + "\n");        
    
    for (int i = 0; i < 4; i++) {
    
        System.out.println("First: " + base);
        // decreasing 60 minutes from hour
        base.decrementaMinuti(60);
    
        System.out.println("After: " + base);
        hours.add(0, base); // here you add the same instance multiple times
    

    You must create a new Ora instance before adding it to the List:

    Ora base = hours.get(0);
    System.out.println("Base: " + base + "\n");        
    
    for (int i = 0; i < 4; i++) {
    
        System.out.println("First: " + base);
        Ora newOra = new Ora (...); // consider having a copy constructor that
                                    // would accept base and copy its data
        // add code to update newOra to contain the same data as base
    
        // decreasing 60 minutes from hour
        newOra.decrementaMinuti(60);
    
        System.out.println("After: " + newOra);
        hours.add(0, newOra); // add the new instance