Results 1 to 15 of 15
  1. #1
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Deleting an object from an array (from index , count)

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

    So, I need to delete the count objects starting at the given index. I have to make sure to compress myObjects and update currentObject (Someone told me that I can't compress an array so, if you agree with that then don't worry about the compress part). I 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 everything 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);
    }
    }

    So far, It's deleting what it has to delete but it doesn't return the currentObject appropriately

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: Deleting an object from an array (from index , count)

    You are not using index properly in your deleteObjects method. First, it doesn't make sense that index should be less than count. Suppose you want to delete
    3 objects starting at index location 10? What you want to insure is that the number of objects to be deleted starting at the index doesn't exceed the size of the array. As for not properly updating the currentObject, what object is that supposed to be? The first in the array? The last?

    And you cannot compress arrays but you can allocate a new one of proper size and copy old to new.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    So, how about this:

    I will show you another method similar to the deleteObjects method ( The question still the same, I just want to give you an Idea about what I want to do)


    public void deleteObject(int index) {
    if (index < currentObject) {
    // yes, index is valid
    // compress the array
    for ( int i = index ; i < (myObjects.length - 1) ; i++ ) {
    myObjects[i] = myObjects[i+1];
    } // end for
    // update currentObject
    currentObject--;
    } else {
    System.out.println("ObjectArray.deleteObject() - Invalid index = " + index +
    " when myObjects only has " + currentObject + " entries");
    } // end if
    }


    public void deleteObjects(int index, int count) {

    if (index < currentObject){

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


    }
    currentObject--;
    currentObject = myObjects.length - (count * index);


    }else {
    System.out.println("ObjectArray.deleteObjects() - Invalid index = " + index +
    " when myObjects only has " + currentObject + " entries");
    } // end if

    So, the deleteObject(int index) does works. But, the deleteObjects(int index, int count) doesn't work.

    I was trying make a clone of the deleteObject(int index) but adding the int count.
    Last edited by maduro; 11-10-2013 at 02:40 AM. Reason: It show more details

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: Deleting an object from an array (from index , count)

    Quote Originally Posted by maduro View Post
    So, how about this:
    Well, you tell me. Does it do what is expected?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    does works but is not moving the objects properly. =/

    It does delete from index to count....
    but when i test that the rest of my list were place in the right place it doesn't works

    maybe the problem is just one line..

  6. #6
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    Any suggestions please?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: Deleting an object from an array (from index , count)

    Please place your code between code tags [code][/code]. It allows the formatter to format your code and make it easier to read.

    Java Code:
    public void deleteObjects(int index, int count) {
    if (index < currentObject){
        for(int i = count; i < myObjects.length - 1; i++){
            myObjects[i] = myObjects[i + 1];
        }
        currentObject--;
        currentObject = myObjects.length - (count * index);
    }else {
        System.out.println("ObjectArray.deleteObjects() - Invalid index = " + index +
          " when myObjects only has " + currentObject + " entries");
    } // end if
    This won't work either. What you really need to do is to use a pencil and paper and draw an array with number in it. Then write down your index and count values and see how it should work. Then, once you get it working, transfer it to code. Your above algorithm only
    "removes" a single value. The one starting at location count.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    So do you mean that if i have deleteObjects(2,9) it's only deleting at index 9?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: Deleting an object from an array (from index , count)

    Yes! You are not using index at all, only count.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    May I ask ..Do i need another loop?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: Deleting an object from an array (from index , count)

    Nope! Think about it this way. You have a starting index of 9 and a count of 3. Since arrays are zero based, the length of the array needs to be at least 10 + 3 or 13. Assume it is 20. Now you want to copy everything from index 9 + 3 or 12 to index 9. This will then copy 12 to 9. then 13 to 10, then 14 to 11. So you are effectively deleting items 9,10,and 11. Just make certain you don't exceed the array length. Like I said earlier, it helps to derive an algorithm first on paper.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    okey ...I fix something but I think that my loop is having problem:

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

  13. #13
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    I get your point:

    transfer:[index + count] To [index]

    but it doesn't works well but if I put
    [index] = [index + count] it does work but my currentObject doesn't update

  14. #14
    maduro is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Deleting an object from an array (from index , count)

    Hey...
    Thank you for your time....

    now code it's working with my demo and you was right! ..paper and pen fixed it everything =)

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: Deleting an object from an array (from index , count)

    Congratulations! And you're welcome.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Help with deleting/printing linkedlist<object>
    By delvinv in forum New To Java
    Replies: 3
    Last Post: 05-02-2012, 09:43 AM
  2. Deleting an element in an array
    By rania.idan in forum New To Java
    Replies: 22
    Last Post: 12-05-2010, 10:34 PM
  3. deleting an element from an array
    By moamen in forum New To Java
    Replies: 11
    Last Post: 01-03-2010, 05:38 PM
  4. Deleting from an array
    By Dieter in forum Advanced Java
    Replies: 13
    Last Post: 09-25-2009, 09:27 AM
  5. Deleting from an object
    By vitaminz in forum New To Java
    Replies: 7
    Last Post: 08-10-2008, 03:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •