How to remove desired element from an arraylist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shiniskumar
    New Member
    • Feb 2007
    • 58

    How to remove desired element from an arraylist

    ive got an int variable whose value can change.
    according to the value of that variable i have to delete the corresponding elenment from an arraylist

    ie if var=5, ive to delete the 5th element of the arraylist. Can i do this without iterating the arraylist
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shiniskumar
    ive got an int variable whose value can change.
    according to the value of that variable i have to delete the corresponding elenment from an arraylist

    ie if var=5, ive to delete the 5th element of the arraylist. Can i do this without iterating the arraylist
    Code:
     list.remove(var);

    Comment

    • shiniskumar
      New Member
      • Feb 2007
      • 58

      #3
      Originally posted by r035198x
      Code:
       list.remove(var);

      I cant use remove() for an integer argument. it allows only objects.

      Help Please

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by shiniskumar
        I cant use remove() for an integer argument. it allows only objects.

        Help Please
        From the API

        Object remove(int index)
        Removes the element at the specified position in this list

        Comment

        • shiniskumar
          New Member
          • Feb 2007
          • 58

          #5
          Originally posted by r035198x
          From the API
          ive got the code as follows
          My collection consists of 10 elements
          for ( int i=0;i<5;i++){
          coll.remove(new Integer(i));
          }

          but using this code im not able to delete the contents of the collection.
          Y is it like tat
          Please help

          Comment

          • shiniskumar
            New Member
            • Feb 2007
            • 58

            #6
            hello..

            can anybody give me a soln to my problem?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by shiniskumar
              ive got the code as follows
              My collection consists of 10 elements
              for ( int i=0;i<5;i++){
              coll.remove(new Integer(i));
              }

              but using this code im not able to delete the contents of the collection.
              Y is it like tat
              Please help
              If you are using a collection and not a list then consider making it an arraylist first before removing using indices. The Collection interface only defines remove(Object o) where you specify the actual object that you want to remove. To remove by specifying the index you can use ArrayList.

              Comment

              • shiniskumar
                New Member
                • Feb 2007
                • 58

                #8
                Originally posted by r035198x
                If you are using a collection and not a list then consider making it an arraylist first before removing using indices. The Collection interface only defines remove(Object o) where you specify the actual object that you want to remove. To remove by specifying the index you can use ArrayList.
                Ive made my list an ArrayList

                coll=new ArrayList();
                Then i did the above code.
                Still i cannot remove the specified element

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by shiniskumar
                  Ive made my list an ArrayList

                  coll=new ArrayList();
                  Then i did the above code.
                  Still i cannot remove the specified element
                  You need to store the data in a variable of type ArrayList

                  Code:
                   ArrayList coll=new ArrayList();

                  Comment

                  Working...