possible bug in DefaultCategoryDataSet.getValue()

A discussion forum for the JCommon class library.
Locked
jim moore

possible bug in DefaultCategoryDataSet.getValue()

Post by jim moore » Fri Feb 01, 2002 8:10 pm

On line 272 of DefaultCategoryDataset, you make the call:

int categoryIndex = Arrays.binarySearch(categories, category);

The javadocs for Arrays.binarySearch(Object[] a, Object key) state:

"The array must be sorted into ascending order according to the natural ordering of its elements (as by Sort(Object[]), above) prior to making this call. If it is not sorted, the results are undefined."

If categories is an array of Strings and category is a String, if categories is not in alphabetic order, a negative index is returned, even if category is present in categories.

A better solution would be (and the one I am using to get this to work):

int categoryIndex = findCategoryIndex(categories, category);

where findCategoryIndex is defined as:

private int findCategoryIndex(Object[] categories, Object category) {
for (int i = 0; i < categories.length; i++) {
if (categories.equals(category)) return i;
}
return -1;
}

jim moore

Re: possible bug in DefaultCategoryDataSet.getValue()

Post by jim moore » Fri Feb 01, 2002 8:15 pm

Though i forgot to mention it, this is referring to version 0.5.3 of JCommon.

David Gilbert

Re: possible bug in DefaultCategoryDataSet.getValue()

Post by David Gilbert » Fri Feb 01, 2002 10:39 pm

Hi Jim,

Thanks for the bug report and the fix. A couple of other people have reported it also...I better fix it before anyone else notices my sloppy coding!

Regards,

DG.

Locked