In Java, why this
int n = 100000;
int[] array = new int[n + n / 2];
int[] arrayzip = new int[n];
System.arraycopy(array, 0, arrayzip, 0, n);
hashmap.put(x, arrayzip);
and this
int n = 100000;
int[] array = new int[n + n / 2];
hashmap.put(x, new int[n]);
System.arraycopy(array, 0, hashmap.get(x), 0, n);
take the same amount of memory? In the first code sample an extra array is created.
[EDIT] Alright, now the second question:
for (int i = 0; i < 100; i++) {
int n = 100000;
int[] array = new int[n + n / 2];
int[] arrayzip = new int[n];
System.arraycopy(array, 0, arrayzip, 0, n);
hashmap.put(x, arrayzip);
}
What if we put any sample of code inside a loop, then arrayzip
is created 100
times and then all 100
references are gone due to being scoped in a loop. Aaa, now I understand, so objects new int[]
are created on the heap but they are not lost (garbage collected) because they stored their references in a out-of-scope object hashmap
. Cool, I understand!