Results 1 to 6 of 6
- 01-07-2013, 06:11 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
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!
- 01-07-2013, 06:15 PM #2
Re: Removing specific values from an ArrayList beginning at a specified index value
Have you gone through the API for ArrayList?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-07-2013, 06:28 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
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.
- 01-07-2013, 07:40 PM #4
Re: Removing specific values from an ArrayList beginning at a specified index value
Can you post the code that you have in place now?
- 01-07-2013, 09:43 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
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.
- 01-07-2013, 10:10 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,539
- Rep Power
- 11
Re: Removing specific values from an ArrayList beginning at a specified index value
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 just reviewed it again, and tried using the remove method and it works.
I agree with sehudson: post code and describe the observed and desired behaviour.
Similar Threads
-
Adding an item to arrayList at a specific index
By randoms:) in forum New To JavaReplies: 2Last Post: 05-11-2011, 04:25 PM -
[HELP] Removing objects from an ArrayList!
By bpx95 in forum New To JavaReplies: 4Last Post: 04-30-2011, 05:54 AM -
Removing values from an array in a list.
By BennyJass in forum AWT / SwingReplies: 2Last Post: 01-17-2011, 07:00 AM -
Removing the double values?
By Lund01 in forum Advanced JavaReplies: 13Last Post: 11-17-2010, 11:34 AM -
Removing from an ArrayList while looping thru
By pahiker in forum New To JavaReplies: 7Last Post: 06-22-2010, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks