I'm sorry if this is an obvious question but neither Google or a search here led me to an answer.
Is there a way to remove an array entirely?
I want the opposite of int[] array = new int[5]
Java arrays do not provide a direct remove method to remove an element. In fact, we have already discussed that arrays in Java are static so the size of the arrays cannot change once they are instantiated. Thus we cannot delete an element and reduce the array size.
You can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically.
Say you call:
void Foo(){ int[] a = new int[5]; }
In C# there is no way to undefine the variable a
. That means a
will be defined in Foo
even if you set a
to null. However, at the end of Foo
a
will fall out of scope. That means no code can reference it, and the garbage collector will take care of freeing the memory for you the next time it runs, which might not be for a long time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With