ArrayList.add (idx, element) damages another array element
I am running a program that:
- Create
ArrayList
- Add an element at the top of this array
- Create a "base" element to be shrunk.
- Then with a for loop, decrease the base element and add it to the top of the array (index 0)
- Unexpectedly, the output I get is scary
Please don't worry about some Italian word:
-
"ora" means hour, therefore
Ora hour = new Ora(6,34) //--> 6:34
-
"decreeMinuti" decreases minutes from hour
This is the complete 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 result 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]
- - - - - - - -
There is an hour 7:00
5 times in the last block of output where I never add the same hour twice.
I am asking : why (as you can see) adding a vertex element of this array causes after elements to be 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 follow is really appreciated.
source to share
You add 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 instance Ora
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
source to share
You add the same object Ora base = hours.get(0)
, and you just change the contents of the object. However the same link. ArrayList allows you to add the same object reference multiple times, but the entries in the ArrayList actually refer to the same object and to the same data memory area. This is why everyone prints the same result.
source to share