Let's say that i have an array of objects.

So, I need to delete the count objects starting at the given index. Make sure to compress myObjects and update currentObject. You must also verify that the given index exists in myObjects and that there are count entries starting from there.

This is what I got:
NOTE: I will just show part of my program, because it's a long program but everthing is working well so far...

First: Those are my attributes:

public class ObjectArray {

private final int INITIAL_ARRAY_SIZE = 10;
private final int ARRAY_EXPAND = 20;
private Object[] myObjects = new Object[INITIAL_ARRAY_SIZE];
private int currentObject;

And this is my code:

public void deleteObjects(int index, int count) {

        if (index < count){

        for(int i = count; i < myObjects.length - 1; i++){
            myObjects[i] = myObjects[i + 1];
    }
        currentObject = myObjects.length - (count * index);
    }
}

NOTE: I asked one similar question before but with deleteObject(int index) and i fixed it.

Thanks

Is there something wrong with my question? Because it has 14 views but 0 reply =(

Please, let me know if you notice something wrong with it

code update:

public void deleteObjects(int index, int count) {
if (index < myObjects.length){
for(int i = count; i < myObjects.length - 1; i++){
myObjects[i] = myObjects[i + 1];
}
currentObject = myObjects.length - (count * index);
}
}

This is very "clunky" code. It does not reset the size (last element) of the array. Use a vector<Object> class and then you can simply call the remove(index) method on it, and all the internal bookkeeping will be done for you. IE, this would be your code:

public class ObjectArray {
private vector<Object> myObjects;
.
.
.
    public void deleteObjects(int index, int count)
    {
        if (index < count){
        for(int i = index; i < index + count; i++)
        {
            myObjects.remove(i);
        }
    }
};

However, if you are trying to emulate how C/C++ would deal with a raw array, then this isn't what you want, although with C++ I would STILL use a vector<type> class these days.

how about if someone will input index 7 and count 2?

Based on what you have posted before, the first version (delete one element) is not yet working - it doesn't check the index and it doesn't maintain the currentIndex variable correctly. Maybe you should be certain that is fixed before moving on?

Anyway, I don't want to be discouraging, but here you posted four lines of executable code, none of which makes sense. Your problem seems to be with the problem's logic rather than with Java syntax - in which case I strongly recommend that you move away from your computer and work through some examples on a piece of paper. Start with an array of values and the currentIndex variable, chose one to delete, and work out and write down the correct final values. Do this a few times and study how you are doing it. That's the logic you then need to code in Java. You can also use those as test cases to check your final code.
(Working throiugh an algorithm on paper first isn't just a novice thing... the more experienced the programmer the more likely they are to use techniques like that!)

@james

yes, I made my deleteObject(one element work) =) ...but i still having problem with the second

deleteObjects(int index, int count)
if (index < currentObject){
    int del = count + index;
    for (int i = 0; i <  count; i++) {// Delete count number of objects.
        //myObjects[index] = myObjects[index+ count];
        deleteObject(index);

    }

    }

but still having an error if I test more probabilities.

OK.
Line 1: incorrect syntax
Line 2: what's that if test for? Certainly violates the spec
Line 3: what's the point of an unused variable?
Line 4: assuming that the single delete is working perfectly, this line is OK.

Misc: You didn't verify index/count as required.

Sorry - I coded a bug there! :-( Here is corrected code:

public class ObjectArray {
private vector<Object> myObjects;
.
.
.
    public void deleteObjects(int index, int count)
    {
        for(int i = 0, j = myObjects.size(); i < count && index < j; i++, j--)
        {
            myObjects.remove(index);
        }
    }
};

This should (I hope) work better - untested code! :-)

@James
I tried a while loop but it doesnt work ...i also delete the unused variable....

And i did try to fix the if statement but i just trying to test that the index and count are valid

Just trying things at random is a v e r y slow way to write a correct program. I still recommend that you do some examples on paper and get the logic 100% clear in your own head before going anywhere near a computer.

Thanks guy! it's works!
James was right lol using paper and pen fixed everyting
I don't know if RubberMan was right but thanks for your time guy!!!

Thank you....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.