The Artima Developer Community
Sponsored Link

Java Answers Forum
Do Vectors search and remove in log(n) time?

2 replies on 1 page. Most recent reply: Oct 29, 2004 12:06 PM by Philip Foulkes

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Gibran Shah

Posts: 27
Nickname: gibby
Registered: Jun, 2004

Do Vectors search and remove in log(n) time? Posted: Oct 28, 2004 3:27 PM
Reply to this message Reply
Advertisement
Hi,

I'm working on an application using a Vector to store a sorted list of numbers. Since it's sorted, it would be advantageous to use a log(n) algorithm for searching and removing items. So I'm wondering if the Vector.contains(Object) and Vector.remove(Object) methods know to search for items in a divide-and-conquer manner when it's sorted. I'd use these methods if this is so. Otherwise, I'm wondering if it would make my program run faster if I were to create my own contains(Object) and remove(Object) methods for searching and removing. Does anyone know what kind of algorithms the Vector class uses for these tasks?

Gib


Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Re: Do Vectors search and remove in log(n) time? Posted: Oct 29, 2004 12:03 PM
Reply to this message Reply
I'm not too sure if the Vector class contains any fast searching techniques, but the Arrays class does:

Vector v1 = new Vector ();

.
.
.

int index = Arrays.binarySearch (v1.toArray(), whateverObject);

Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Re: Do Vectors search and remove in log(n) time? Posted: Oct 29, 2004 12:06 PM
Reply to this message Reply
Once you know the index of a particular item, then it is simple to remove it using:

v1.remove (index);

Flat View: This topic has 2 replies on 1 page
Topic: my session is not get invalidate even after executing it in many way Previous Topic   Next Topic Topic: i don't understand  i ?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use