Home > Archive > Java Help > March 2006 > Good way to remove Vector elements from end?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Good way to remove Vector elements from end?
|
|
| Jim Bancroft 2006-03-29, 4:09 am |
| Hi,
I'd like to remove all elements in my vector from, say, index i to the end.
Is there a good way of doing this? I have a kludge which may or may not do
the job (haven't tested yet), but I'm thinking there's a better way:
for(int x=(myVector.size()-1); x >= i; x--){
myVector.removeElementAt(x);
}
Even looking at that makes me squirm, and there's got to be another
approach. If you know of one I'm all ears. Thanks!
| |
|
|
| Oliver Wong 2006-03-29, 7:04 pm |
|
"Jim Bancroft" <sdfsk@sfd.com> wrote in message
news:6QpWf.6639$4L1.3635@newssvr11.news.prodigy.com...
> Hi,
>
> I'd like to remove all elements in my vector from, say, index i to the
> end. Is there a good way of doing this? I have a kludge which may or may
> not do the job (haven't tested yet), but I'm thinking there's a better
> way:
>
> for(int x=(myVector.size()-1); x >= i; x--){
> myVector.removeElementAt(x);
>
> }
>
> Even looking at that makes me squirm, and there's got to be another
> approach. If you know of one I'm all ears. Thanks!
http://java.sun.com/j2se/1.5.0/docs...or.html#setSize(int)
- Oliver
| |
| Thomas Hawtin 2006-03-29, 7:04 pm |
| Jim Bancroft wrote:
>
> I'd like to remove all elements in my vector from, say, index i to the end.
> Is there a good way of doing this? I have a kludge which may or may not do
> the job (haven't tested yet), but I'm thinking there's a better way:
>
> for(int x=(myVector.size()-1); x >= i; x--){
> myVector.removeElementAt(x);
>
> }
In general you can remove a range from a List with subList and then clear:
myList.subList(from, to).clear();
It does create one tiny little object. These days the cost of allocating
a tiny object is small.
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
| |
| Roedy Green 2006-03-29, 7:04 pm |
| On Wed, 29 Mar 2006 06:30:26 GMT, "Jim Bancroft" <sdfsk@sfd.com>
wrote, quoted or indirectly quoted someone who said :
>I'd like to remove all elements in my vector from, say, index i to the end.
>Is there a good way of doing this? I have a kludge which may or may not do
>the job (haven't tested yet), but I'm thinking there's a better way:
>
> for(int x=(myVector.size()-1); x >= i; x--){
> myVector.removeElementAt(x);
>
> }
ArrayList.removeRange(int fromIndex, int toIndex)
is protected so you can't use it.
Your way is the best I know of that does the least shuffling.
See http://mindprod.com/jgloss/arraylist.html#REMOVING
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
| |
| Roedy Green 2006-03-29, 7:04 pm |
| On Wed, 29 Mar 2006 15:43:08 +0100, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :
>In general you can remove a range from a List with subList and then clear:
>
> myList.subList(from, to).clear();
clearing and removing are different operations. Is it possible to use
sublist to remove?
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
| |
| Thomas Hawtin 2006-03-29, 7:04 pm |
| Roedy Green wrote:
> On Wed, 29 Mar 2006 15:43:08 +0100, Thomas Hawtin
> <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
> who said :
>
>
> clearing and removing are different operations.
> sublist to remove?
clear removes all the elements from a collection. "Clear" does sound
like nulling out, but isn't.
> Is it possible to use
> sublist to remove?
"The returned list supports all of the optional list operations
supported by this list."
<http://download.java.net/jdk6/docs/...st.html#subList(int,
int)>
(Which, coincidentally, also contains almost exactly the same example as
I wrote above.)
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
| |
| Thomas Hawtin 2006-03-29, 7:04 pm |
| Roedy Green wrote:
>
> ArrayList.removeRange(int fromIndex, int toIndex)
> is protected so you can't use it.
At least not unless you subclass.
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
|
|
|
|
|