Hey guys
java.util.Arrays.binarySearch(Object[] a, Object key, Comparator c)
exchanges a[i] and key, this can lead to ClassCastExceptions as shown in
ComparatorTest.java. Arrays.patch fixes it.
Changelog suggestion:
2006-12-13 Marco Trudel <[EMAIL PROTECTED]>
* classpath/java/util/Arrays.java (binarySearch(Object[] a, Object key,
Comparator c)):
Fix swapped objects inCollections.compare(...) call.
Comments? Can someone commit this for me (I have copyright assignment)?
thanks
Marco
import java.util.Arrays;
import java.util.Comparator;
public class ComparatorTest
{
public static void main(String[] args)
{
Comparator comp = new Comparator()
{
public int compare(Object o1, Object o2)
{
int i1 = Integer.parseInt((String)o1);
int i2 = ((Integer)o2).intValue();
return (i1 == i2 ? 0 : (i1 < i2 ? -1 : 1));
}
};
Object[] o1 = { "22", "23", "24" };
Object o2 = new Integer(23);
// has to print "1", throws a java.lang.ClassCastException
System.out.println(Arrays.binarySearch(o1, o2, comp));
}
}
Index: classpath/java/util/Arrays.java
===================================================================
--- classpath/java/util/Arrays.java (Revision 119819)
+++ classpath/java/util/Arrays.java (Arbeitskopie)
@@ -370,7 +370,7 @@
while (low <= hi)
{
mid = (low + hi) >>> 1;
- final int d = Collections.compare(key, a[mid], c);
+ final int d = Collections.compare(a[mid], key, c);
if (d == 0)
return mid;
else if (d < 0)