Suppose I have a number N. N will be the size of the array.
int numArray [] = new numArray[N];
However, the contents of the array will contain any other number from 1 to positive N. This means that the entire array of size N will not be filled after this cycle. So after the for loop, I want to trim (or resize) the array so that there are no more empty slots in the array.
Example:
Let, say, N = 5; This means that after the for loop, every other number from 1 to 5 will be in the array as follows:
int arr [] = new int [N];
int arr[0]=1;
int arr[1]=3;
int arr[2]= null;
int arr[3]= null;
int arr[4]= null;
Now I want to trim (or resize) after the for loop so that indexes that contain zero disappear, and then the array should be:
int arr[0]=1;
int arr[1]=3;
The size of the array is now 2.
source
share