Article View: borland.public.jbuilder.java.language
Article #15073Pb with Collections.binarySearch algorithm
From: Gemplus Software
Date: Thu, 15 Jul 1999 00:00
Date: Thu, 15 Jul 1999 00:00
45 lines
1745 bytes
1745 bytes
Hello Everybody,
Here is my code :
public class TestPerf {
Vector myV;
public Vector getVector() {
return myV;
}
public TestPerf() {
myV = new Vector();
}
public static void main(String[] args) {
TestPerf testPerf = new TestPerf();
final int nNbIter = 30000;
testPerf.invokedStandalone = true;
for (int i = 0 ; i < nNbIter; i++) {
String myStr = "toto" + String.valueOf(i);
testPerf.getVector().addElement(myStr);
}
String mySearchStrMid = "toto14999";
int nPos = 0;
//Object[] mySA = testPerf.getVector().toArray();
//Arrays.sort(mySA);
//nPos = Arrays.binarySearch(mySA, mySearchStrMid);
nPos = Arrays.binarySearch(testPerf.getVector().toArray(),
mySearchStrMid);
if ( nPos >= 0 )
System.out.println("Value Mid in Vector = " +
(String)testPerf.getVector().toArray()[nPos]);
}
}
I am suprised because, in the JDK 1.2 docs, they say that you have to
sort before applying the binarySearch. So,what's the matter ? If I do
the same within a LinkedList it fails (Exception generated) and works if
I do a sort before calling the binarySearch algorithm .
Any Idea ?
Technical Details
Message-ID:
<378DF275.418B1171@gemplus.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,9cf805eaebbed18,start X-Google-Attributes: gidf74b3,public From: Gemplus Software <gemplus.software@gemplus.com> Subject: Pb with Collections.binarySearch algorithm Date: 1999/07/15 Message-ID: <378DF275.418B1171@gemplus.com> X-Deja-AN: 501343959 Content-Transfer-Encoding: 7bit Organization: Another Netscape Collabra Server User X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language