I am using array for storing a kind of object. I created an array of some fixed size
int arr[]=new int[n];
Now after processing this array i want to free upto 75% of the memory from this array(now only n/4 elements are useful). So what my question is, since n is very large and i wish not to hold larger memory than useful, How can i reduce size of array at runtime without copying to new array of size n/4(Is it even possible or not?).
It's not possible. You cannot change the length of an array after you initialise it.
What you can do is create another array with suitable size and make this large array eligible for Garbage Collector.
Best is to use ArrayList
if you are allowed to do that.
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