maduro
Nov 9, 2013, 08:08 PM
Public void deleteObjects(int index, int count) {
if (index < currentObjectt){
for(int I = count; I < myObjects.length - 1; I++){
myObjects[i] = myObjects[i + 1];
}
currentObject = myObjects.length - (count * index);
}
}
how come that I am not using (int index) at all?
9hththt2
Nov 10, 2013, 10:10 AM
What do you intend to do, exactly?
You can set that array position to NULL; is that what you want?
myObjects[idx] = null;
9hththt2
Nov 10, 2013, 11:13 AM
Please let me help you.
Considering the name of your method, "deleteObjects", I think what you are trying to do is delete "N objects" from an array, starting at a given index. Is that right?
I don't understand what is your "currentObject" variable for, though.
If what you are trying to do is this (what I understood), then you should consider looking at it this way:
First you need to check if the " index " parameter is valid for that array "myObjects".
If the "index" is out of your array's bounds (if it's less than 0, or higher than the length of your array), then you should RETURN from the method.
You should also check if "index+count-1" (which would be the last index that you want to "remove" from the array) is in the array's bounds. If not, you can Return from the function too.
You can't remove elements from an array, because arrays have a static dimension. You can only set the positions to NULL, like this:
myArray[i]=null;
But you can "redefine" your old array, by creating a new array, with the correct new dimension, and copying the elements that are important, from the old(original) array, ignoring the ones you wish to "remove". This way, you get a new array without the elements that you wished to remove. Then you just have to do something like:
originalArray = newArray;
Please check this (http://pastebin.com/tush4AHU).
[Java] deleteObjects-Remove "count" integers from array, from index - Pastebin.com (http://pastebin.com/tush4AHU)
I pasted it here for you; it's a solution to this problem that I just made, and hope it helps.