Results 1 to 7 of 7
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why can not I remove an object from an arrayList?

    Hi guys this is my first question in this forum, thanks for the great work.

    Here is my question:

    I have an ArrayList called shipGridPlaces full of integers

    if I use this code:
    if(shipGridPlaces.contains(k))
    {
    int j = setup.shipGridPlaces.indexOf(k);
    System.out.println("A hit!");
    setup.shipGridPlaces.remove(j);
    }


    I can remove the integer k from the ArrayList with no problems.

    But how I can directly remove an integer from an ArrayList?

    Why doesn't this work:

    if(shipGridPlaces.contains(k))
    {
    setup.shipGridPlaces.remove(k);
    }

    This code actually removes the index number k, not the k itself. So it looks for say k= 45, tries to remove the 45.th element in the ArrayList? Is there any way to remove the object 45 from the ArrayList directly without going with my first solution?

    I hope my question is clear,
    Thanks for your helps.

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can not I remove an object from an arrayList?

    I found this:

    import java.util.ArrayList;

    public class RemoveArrayListElement {

    public static void main(String[] args) {

    ArrayList<String> arlist=new ArrayList<String>();

    //<E> it is return type of ArrayList

    arlist.add("First Element"); // adding element in ArrayList
    arlist.add("Second Element");
    arlist.add("Third Element");
    arlist.add("forth Element");
    arlist.add("fifth Element");

    // remove array list element by index number
    arlist.remove(3);

    // remove ArrayList element by Object value
    arlist.remove("fifth Element");

    // get elements of ArrayList
    for(int i=0;i<arlist.size();i++)
    {
    System.out.println("ArrayList Element "+i+" :"+arlist.get(i));
    }
    }
    }

    But what if my object is an integer itself? How do I remove that directly?

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can not I remove an object from an arrayList?

    I changed the type of k from int to Integer,
    solved my problem.

    Thanks guys.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Why can not I remove an object from an arrayList?

    what if my object is an integer itself? How do I remove that directly?
    ArrayLists can only contain objects. You would not be able to put an int into an ArrayList.
    You would use one of the wrapper classes: Integer.

    Is there any way to remove the object 45
    What is "object 45"? What type of object?

    There is a confusion caused by java's autoboxing feature. The compiler will automatically convert an int to an Integer when the syntax requires an object. But it does not convert an int if an int value could be valid because there is an overloaded method.
    anArrayList.add(23); // converts 23 to new Integer(23) and adds that object
    anArrayList.remove(23); // this will NOT covert the 23 to an Integer. It will remove the 23rd element

    You might think add and remove could use the same arguments and work the same way but you'll be fooled by autoboxing.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can not I remove an object from an arrayList?

    Hi Norm thanks for the answer.

    Actually when the k in arraylist.remove(k) is an Integer (not an int), then it removes k really not the kth element in the arraylist.

    Thanks.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Why can not I remove an object from an arrayList?

    Yes there is a difference between the two overloaded methods as I tried to explain:
    remove(int)
    vs
    remove(object)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Why can not I remove an object from an arrayList?

    The joy of auto-boxing.

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. ArrayList and remove()
    By Pragz in forum New To Java
    Replies: 1
    Last Post: 04-21-2011, 01:42 AM
  3. need help with the remove method on arrayList
    By ShinTec in forum New To Java
    Replies: 5
    Last Post: 02-16-2010, 09:38 AM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM

Posting Permissions

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