|
array problem
I have this prob for HW:
Consider an array
int [] nums = new int[30];
int numsCount = 0
for (int index = 0; index < 10; index++)
{
nums[index] = 10 * index;
numsCount++;
}
We want to add and delete elements of this array. Initially we loaded 10 elements.
To add an element, we just need to say
nums[numsCount] = 41;
numsCount++;
and that adds an element and ups the number of "real" elements in our array.
Now we want to delete an element, say the 5th element. But we also need to decrease the
numsCount and do something to replace this 5th element with with the last element and zero out
the last element (or not) so that if we look at the array that we are managing and report on
its valid contents, we just get the numsCount number of elements.
So ----
Write a method deleteElement that accepts an array and a number (int) of "true" elements in the array and
a number delnum that is non-negative and less than the number of "true" elements. deleteElement will
delete the delnum element (double check that it is in the proper range) and replace it with the last "real"
element in the array.
To try: set the last element in the array to some default value.
------------------------
ii tried compiling a lot of codes and none of them seem to work or really make sense to me. ANy help is greatly appreciated.
|