Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Good way to remove Vector elements from end?

Thread view: 
Jim Bancroft - 29 Mar 2006 07:30 GMT
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!
opalpa@gmail.com opalinski from opalpaweb - 29 Mar 2006 12:00 GMT
myVector.setSize(i);

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Thomas Hawtin - 29 Mar 2006 15:43 GMT
> 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
[quoted text clipped - 4 lines]
>
>   }

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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 29 Mar 2006 19:03 GMT
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?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 29 Mar 2006 18:18 GMT
> On Wed, 29 Mar 2006 15:43:08 +0100, Thomas Hawtin
> <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 4 lines]
> 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/api/java/util/List.html#subList(int,
int)>

(Which, coincidentally, also contains almost exactly the same example as
I wrote above.)

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Oliver Wong - 29 Mar 2006 16:16 GMT
> Hi,
>
[quoted text clipped - 10 lines]
> 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/api/java/util/Vector.html#setSize(int)

   - Oliver
Roedy Green - 29 Mar 2006 18:58 GMT
>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
[quoted text clipped - 4 lines]
>
>  }

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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 29 Mar 2006 18:19 GMT
>  
> ArrayList.removeRange(int fromIndex, int toIndex)
> is protected so you can't use it.

At least not unless you subclass.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.