Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Arrays.sort throws exception

Reply
Thread Tools

Java Arrays.sort throws exception

 
 
Philipp
Guest
Posts: n/a
 
      05-15-2008
Tom Anderson wrote:
> On Wed, 14 May 2008, Owen Jacobson wrote:
>
>> On May 14, 10:37 am, captain <(E-Mail Removed)> wrote:
>>> When the number of elements gets above around 2700 the Arrays.sort
>>> inside the Collections.sort throws an exception:
>>>
>>> java.lang.ArrayIndexOutOfBoundsException
>>>
>>> Any ideas?
>>>
>>> Thanks

>>
>> I've seen this happen when the Comparator (or Comparable)
>> implementation is not consistent.

>
> My money is on some kind of bug in compareTo too. I don't see what else
> it can be.


Could it be that the comparator must be consistent with equals for the
sort to work?

Phil
 
Reply With Quote
 
 
 
 
Philipp
Guest
Posts: n/a
 
      05-15-2008
Lew wrote:
> Philipp wrote:
>> Tom Anderson wrote:
>>> On Wed, 14 May 2008, Owen Jacobson wrote:
>>>
>>>> On May 14, 10:37 am, captain <(E-Mail Removed)> wrote:
>>>>> When the number of elements gets above around 2700 the Arrays.sort
>>>>> inside the Collections.sort throws an exception:
>>>>>
>>>>> java.lang.ArrayIndexOutOfBoundsException
>>>>>
>>>>> Any ideas?
>>>>>
>>>>> Thanks
>>>>
>>>> I've seen this happen when the Comparator (or Comparable)
>>>> implementation is not consistent.
>>>
>>> My money is on some kind of bug in compareTo too. I don't see what
>>> else it can be.

>>
>> Could it be that the comparator must be consistent with equals for the
>> sort to work?

>
> That would be Owen's point.


I understood it as: "the comparator itself is not fulfilling its
contract". Contract which is (for the OP, see also "Effective Java" by
Bloch, Item 11):

Transitive:
(x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0

Symmetric:
sgn(x.compareTo(y)) == -sgn(y.compareTo(x))
in particular x.compareTo(y) must throw an exception if and only if
y.compareTo(x) throws an exception

Coherence:
x.compareTo(y) == 0 implies that
sgn(x.compareTo(z)) == sgn(y.compareTo(z))

Phil
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-15-2008
Lew wrote:
> captain said :
>>> loop:
>>> adminItems.add(administeredItem);

>
> captain, why are you using a label? That is highly unidiomatic.
>


I'm not sure whether it is part of captain's code, and therefore a
label, or outside the code, saying that the add call is in a loop.

I really hope captain is off either writing an SSCCE, or unit testing
the compareTo or compare method.

Patricia
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      05-15-2008
On Wed, 14 May 2008, Eric Sosman wrote:

> captain wrote:
>> This is my very first post and I am not up on the etiquete yet.
>>
>> I realize that the problem must be in my code. Actually, I didn't
>> right this code, I inherited it. I have a lot of programming
>> experience but not in this particular environment.
>> [...]

>
> It is astonishing, or perhaps sobering or depressing, to
> find that someone with "a lot of programming experience" is
> unable to form a coherent problem report nor able to produce
> a usable test case.


I'm guessing that the 'programming' is the type they have on TV. In this
case, probably watched with a beer in hand, packet of cheetos in lap,
trucker cap on head, and drool escaping from mouth.

tom

--
.... but when you spin it it looks like a dancing foetus!
 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      05-15-2008
On May 15, 8:31*am, Lew <(E-Mail Removed)> wrote:
> Philipp wrote:
> > Tom Anderson wrote:
> >> On Wed, 14 May 2008, Owen Jacobson wrote:

>
> >>> On May 14, 10:37 am, captain <(E-Mail Removed)> wrote:
> >>>> When the number of elements gets above around 2700 the Arrays.sort
> >>>> inside the Collections.sort throws an exception:

>
> >>>> java.lang.ArrayIndexOutOfBoundsException

>
> >>>> Any ideas?

>
> >>>> Thanks

>
> >>> I've seen this happen when the Comparator (or Comparable)
> >>> implementation is not consistent.

>
> >> My money is on some kind of bug in compareTo too. I don't see what
> >> else it can be.

>
> > Could it be that the comparator must be consistent with equals for the
> > sort to work?

>
> That would be Owen's point.


Not as such. My point was that identities like a < b && b < c is
implied by and implies that a < c, along with simpler ones like a < b
implies that b > a, must be implemented by the compareTo or compare
method in use. None of the sort implementations in the JRE bother
checking for .equals equality; they rely on .compareTo/.compare
equality.

-o
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-15-2008
On Thu, 15 May 2008 01:23:59 -0400, Lew <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>
>captain, why are you using a label? That is highly unidiomatic.


I presumed pseudocode.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-15-2008
On Thu, 15 May 2008 15:22:57 +0100, Tom Anderson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>
>I'm guessing that the 'programming' is the type they have on TV.


it could be simply some quite different language, like LISP.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-15-2008
On Thu, 15 May 2008 06:33:26 -0700, Patricia Shanahan <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>It is much more likely that the OP's compareTo is inconsistent with
>itself, so that it does not represent a total order.


consider that the sort also uses Comparator.compare where the
algorithm rarely has any connection whatsoever to equals.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      05-15-2008
On Thu, 15 May 2008, Roedy Green wrote:

> On Thu, 15 May 2008 15:22:57 +0100, Tom Anderson
> <(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
> said :
>
>> I'm guessing that the 'programming' is the type they have on TV.

>
> it could be simply some quite different language, like LISP.


I doubt it. Programming is basically the same in all languages, just with
different spelling. The idea that we could diagnose a problem in which
library routine A calls user-written routine B without seeing the text of
routine B is pretty bizarre in any language.

Also, i believe LISPers tend to be towards the smarter end of the spectrum
of programmers, and this guy, well, not so much,

If we widen the definition of 'programming' to mean 'coding', i could
maybe believe he was experienced with HTML, or something else really,
really unlike a programming language. Maybe perl?

tom

--
The real romance is out ahead and yet to come. The computer revolution
hasn't started yet. -- Alan Kay
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-16-2008
Lew wrote:
> Patricia Shanahan wrote:
>> I have sorted arrays by Comparator criteria that are definitely
>> inconsistent with equals, and have never got an exception from sort
>> doing so. There is no requirement that a Comparator order have anything
>> to do with equals.

>
> And yet the Javadocs for Comparator make a big deal out it. Why should
> they do that if it doesn't matter?


It matters, but not for Arrays.sort.

The issue they discuss is a class, such as TreeSet, that is bound by an
contract described in terms of equals, but that will fail to conform to
it unless the Comparator is consistent with equals.

The Arrays.sort contract does not depend on equals, so it can fully
conform to its contract provided the Comparator represents a total order
over the array elements, regardless of whether that order is consistent
with equals.

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is an exception specification needed if the method throws and catchesits own exception? elcapitan666@gmail.com C++ 2 11-27-2007 04:01 AM
Java Regular Expression throws StackOverflow Exception sravan Java 2 09-03-2004 08:58 PM
Re: session.abandon throws 'invalid view state' exception in netscape Cowboy \(Gregory A. Beamer\) ASP .Net 1 05-16-2004 02:31 AM
Exception Exception is not compatible with throws clause in Runnable.run() Chris Miller Java 4 11-22-2003 03:11 PM
XmlValidatingReader throws exception for SAOP-ENV:encodingStyle attribute Himmat Dhange ASP .Net 0 08-26-2003 08:28 PM



Advertisments