Size of arrays

Asked

Viewed 66 times

0

If the theory says that the size of an array cannot 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];

2 answers

4

In the example you gave it should be borne in mind that a is a pointer to an Array and not the Array itself. So the moment you did a = new int[10] the a that previously pointed to an Array of size 5 now now points to the Array of size 10.

And what happened to Array size 5?

Once the array has no one pointing at you, it is ready to be displaced by the Garbage Collector.

  • Makes a lot of sense.

1


You didn’t change the size of the vector, you instated it again with a larger vector size.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.