Results 1 to 6 of 6
  1. #1
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Removing specific values from an ArrayList beginning at a specified index value

    Ok so say I have an ArrayList containing a sentence such as "the brown dog jumped over the yellow cat by the river" where each word is its own element or value in the arraylist. How could I go about removing a specific amount of words from that? Say I wanted to remove all of the "the" in the ArrayList. If I didn't know where all of the "the" were, how would I go about removing them? Also, say I wanted to remove all of the "the" beginning at the index value of 2, how would I also go about doing that? I know how to loop through removing certain elements at index values, however I'm not sure how to remove specific elements in an ArrayList.

    Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Removing specific values from an ArrayList beginning at a specified index value

    Have you gone through the API for ArrayList?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Removing specific values from an ArrayList beginning at a specified index value

    I just reviewed it again, and tried using the remove method and it works. I tried that earlier but it didn't work for some reason. I'm still confused on how I could remove something beginning at the specified index value however.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Removing specific values from an ArrayList beginning at a specified index value

    Can you post the code that you have in place now?

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Removing specific values from an ArrayList beginning at a specified index value

    I anticipate that as you are iterating over the ArrayList and remove an element, that all of the other elements will slide down. This can be slow for large ArrayLists. Also you are likely using the index of the element to remove it and you have to keep in mind that you have to use this same index again because there will be a new element in the same location where you just removed an element.

    If you have a large Collection consider using a LinkedList because deletions will be much faster.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Removing specific values from an ArrayList beginning at a specified index value

    I just reviewed it again, and tried using the remove method and it works.
    No, the remove() method is documented to remove the first occurrence of "the", not all of them as you described in your original post. For that task you could use removeAll(). As far as the range is concerned, check out the subList() method which is there so that you can limit operations to specific portions of the list.

    I agree with sehudson: post code and describe the observed and desired behaviour.

Similar Threads

  1. Adding an item to arrayList at a specific index
    By randoms:) in forum New To Java
    Replies: 2
    Last Post: 05-11-2011, 05:25 PM
  2. [HELP] Removing objects from an ArrayList!
    By bpx95 in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 06:54 AM
  3. Removing values from an array in a list.
    By BennyJass in forum AWT / Swing
    Replies: 2
    Last Post: 01-17-2011, 08:00 AM
  4. Removing the double values?
    By Lund01 in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 12:34 PM
  5. Removing from an ArrayList while looping thru
    By pahiker in forum New To Java
    Replies: 7
    Last Post: 06-22-2010, 08:38 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
  •