Size of arrays

0

If the theory says that the size of an array can not be changed, only the elements. Because when I print the size of a the value is 10 and not 5 or 15?

int[] a;
    a = new int[5];
    a = new int[10];
    
asked by anonymous 21.11.2017 / 01:42

2 answers

1

You did not change the size of the vector, you instantiated it again with a larger size vector.

    
21.11.2017 / 02:02
3

In the example you gave you should keep in mind that a is a pointer to an Array, not the Array itself. So, the moment you made a = new int[10] the a that previously pointed to a size 5 Array now points to the size 10 Array.

And what happened to the size 5 Array?

From the moment that the array has no one pointing at you, it's ready to be deallocated by the Garbage Collector.

    
21.11.2017 / 02:05