Results 1 to 9 of 9
  1. #1
    Matty3z is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default [Help] .. How To remove an element of an array ?

    removed
    Last edited by Matty3z; 12-28-2011 at 02:31 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: [Help] .. How To remove an element of an array ?

    Create a new array that is one index smaller, then copy every index except the one you don't want.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Matty3z is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: [Help] .. How To remove an element of an array ?

    the project i'm working on is array of objects .. each object (contact ) contains Name and Number ..
    and the main array is 1000 length .. it already have some contacts .. about 9 .. so it's not using the whole 1000 .. and it can be added new contacts

    so it will be kinda complex i think to copy it in other one .. cuz the main array have to be changed
    i dunno :S

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: [Help] .. How To remove an element of an array ?

    Okay, then simply shift all of the elements down by an index...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Matty3z is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: [Help] .. How To remove an element of an array ?

    ya .. but how .. i tired but each time i get an exception .. and doesn't work .. :\ ?
    i don't know what's wrong :S

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: [Help] .. How To remove an element of an array ?

    What exception are you getting?

    Instead of trying to change the name and number of each instance, why not just move the instance themselves?

    Hint: array[0] = array[1];
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Matty3z is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: [Help] .. How To remove an element of an array ?

    This one .. >>
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Project.main(Project.java:130)
    
    Process completed.
    ya .. i know it's like that .. but it doesn't work
    did you read my code in 1st post ?

  8. #8
    Matty3z is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: [Help] .. How To remove an element of an array ?

    and the line 130 is same this one on my code here >>
    Java Code:
    frnds0[i].changeName(frnds0[j].getName());

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: [Help] .. How To remove an element of an array ?

    You should compartmentalise your code to make it easy to follow.

    Create a separate utility method just for removing and updating an element from an array.

    You can call that method later from wherever it becomes useful.


    If you want to do it the hard way (with Arrays):
    Java Code:
    public static Object[] removeElement(Object[] objArray, int index) {
        //don't bother if its a bad index or null pointer
        if (objArray != null && index >= objArray.length || index <0 || objArray == null)
            return objArray;
    
        int newSize = objArray.length-1;
    
        //erase over the value by shifting the elements
        for (int i=index; i<newSize; )
            objArray[i] = objArray[++i];
    
        //of course you cannot change the size of the array, so return a new one
        Object result[] = new Object[newSize];
        System.arraycopy(objArray, 0, result, 0, newSize);
        return result;
    }
    If you want to do it the easy way, use List<Object> and ArrayList<Object> like so:
    List<MyObj> myObjList = new ArrayList<MyObj>(Arrays.asList(getMyObjArray()));

    Then to remove an index or an object:
    int x = 1;
    MyObj obj = new MyObj();
    myObjList.remove(obj);
    myObjList.remove(x);

Similar Threads

  1. ArrayList remove an element from the list
    By ralf in forum New To Java
    Replies: 37
    Last Post: 07-12-2011, 07:08 PM
  2. remove element contents
    By oliveira in forum XML
    Replies: 5
    Last Post: 12-07-2010, 08:44 AM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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