Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Java Arrays.sort throws exception

 
 
captain
Guest
Posts: n/a
 
      05-14-2008
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.

Here, a list is populated in a loop and then Collections.sort is
called on that list. I would like to know what the possible problems
may be, what to look for.

Thanks

On May 14, 2:57*pm, Eric Sosman <(E-Mail Removed)> wrote:
> captain wrote:
> > Eric,

>
> > Thank you very much for your response.

>
> > My code:
> > * List adminItems = new ArrayList();

>
> > * loop:
> > * * *adminItems.add(administeredItem);

>
> > * Collections.sort(adminItems);

>
> * * *My code:
>
> * * * * import java.util.ArrayList;
> * * * * import java.util.Collections;
>
> * * * * public class Foo {
>
> * * * * * * public static void main(String[] unused) {
> * * * * * * * * final int COUNT = 2700;
> * * * * * * * * ArrayList<Integer> list = new ArrayList<Integer>(COUNT);
> * * * * * * * * for (int i = 0; *i < COUNT; *++i)
> * * * * * * * * * * list.add(new Integer(
> * * * * * * * * * * * *(int)(Math.random() * Integer.MAX_VALUE)));
> * * * * * * * * Collections.sort(list);
> * * * * * * }
>
> * * * * }
>
> Runs like a champ, both as shown and with COUNT = 1000000.
>
> * * *There's something I'd like you to notice, a difference between
> your code and mine: Mine actually runs, while yours won't even
> compile. *I can get yours to compile and run by "filling in the
> blanks," as it were -- but that probably means that the problem is
> in the blanks, the part you still haven't revealed.
>
> * * *Hint, hint.
>
> --
> (E-Mail Removed)


 
Reply With Quote
 
 
 
 
Andrea Francia
Guest
Posts: n/a
 
      05-14-2008
captain wrote:
> Roedy,
>
> Thank you very much for your response.
>
> My code:
>
> List adminItems = new ArrayList();
>
> loop:
> adminItems.add(administeredItem);
>
> Collections.sort(adminItems);


You don't need to post the library code.

You didn't post yet enough information however.

The code is not a SSCCE (is not compilable).
Where administeredItem is defined?
Why you did not post the loop part?
It's impossible help you if you don't post a code which is compilable.
And it's impossible to help you if the code does not reflect your code
enviroment.

Please read the http://sscce.org and post a SSCEE.

You should also post the Exception stack trace.

Please read in http://mindprod.com/jgloss/newsgroups.html the section
How To Get Newsgroup Responses

Ciao
--
Andrea Francia
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-14-2008
Andrea Francia wrote:
> captain wrote:
>> Roedy,
>>
>> Thank you very much for your response.
>>
>> My code:
>>
>> List adminItems = new ArrayList();
>>
>> loop:
>> adminItems.add(administeredItem);
>>
>> Collections.sort(adminItems);

>
> You don't need to post the library code.
>
> You didn't post yet enough information however.
>
> The code is not a SSCCE (is not compilable).
> Where administeredItem is defined?
> Why you did not post the loop part?
> It's impossible help you if you don't post a code which is compilable.
> And it's impossible to help you if the code does not reflect your code
> enviroment.
>
> Please read the http://sscce.org and post a SSCEE.
>
> You should also post the Exception stack trace.
>
> Please read in http://mindprod.com/jgloss/newsgroups.html the section
> How To Get Newsgroup Responses


Even without its use for communicating with the newsgroup, building an
SSCCE would be a good strategy for this type of problem. The library
sort code is very well tested and widely used, so it is almost certainly
not the problem.

The very fact that you are stuck suggests that the problem is somewhere
you are not looking. In the process of building the SSCCE you will
probably find that removing or simplifying some aspect of your program
makes the problem go away.

Patricia
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2008
On Wed, 14 May 2008 11:32:07 -0700 (PDT), captain
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

> List adminItems = new ArrayList();
>
> loop:
> adminItems.add(administeredItem);
>
> Collections.sort(adminItems);
>
>------------
>Collections.class (Sun Code):
>
> public static void sort(List list) {
> Object a[] = list.toArray();
> Arrays.sort(a); // THROWS EXCEPTION
> ListIterator i = list.listIterator();
> for (int j=0; j<a.length; j++) {
> i.next();
> i.set(a[j]);
> }
> }


You did not tell us the type of administeredItem. I assume for
purposes of argument AdministeredItem.

You want something like this:

List<AdministeredItem> adminItems
= new ArrayList<AdministeredItem>( 100 );

loop:
adminItems.add(administeredItem);

Collections.sort(adminItems);

Your class AdministeredItem class will need to implement
Comparable<AdministeredItem>

See http://mindprod.com/jgloss/comparable.html
for how.

If administeredItem is a String then you need

List<String> adminItems
= new ArrayList<String>( 100 );

The piece of code of most interest is the compareTo method of
AdministeredItem. You did not reveal it yet.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2008
On Wed, 14 May 2008 20:00:05 GMT, Andrea Francia
<(E-Mail Removed)-HERE.gmx.it> wrote,
quoted or indirectly quoted someone who said :

>It's impossible help you if you don't post a code which is compilable.
>And it's impossible to help you if the code does not reflect your code
>enviroment.


The problem is nearly always in the code you DON'T post. That why the
SSCCE. See http://mindprod.com/jgloss/sscce.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2008
On Wed, 14 May 2008 12:38:11 -0700 (PDT), captain
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>This is my very first post and I am not up on the etiquete yet.


It is not etiquette. It is simply a matter of showing us ALL the
relevant code so we can find them problem.

What you are doing is like going to the doctor, wrapped in packing
bubbles with only your hand exposed.

You say "I feel terrible. Dr. Green, you will have to figure out why
my stomach hurts only by examining my hand. I'm sure it has something
to do with my hand, because that is the hand I eat with."

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2008
On Wed, 14 May 2008 12:36:06 -0700 (PDT), captain
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>
>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.


You may not have enough skill to write an SSCCE, but you can post the
entire class, huge and ugly as it may be, and see if you can find
someone willing to eyeball it.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      05-14-2008
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. Arrays.sort relies on the compare
operation producing consistent results for all possible pairs; it
elides operations where it can "guess" the result from previous
comparisons.

Since you haven't shown us what the implementation of Comparable<T>
looks like (the compareTo (T other) and related methods) for whatever
objects you're adding to the collection are, it's very hard to assert
that this is your problem, but it looks *likely* to me.

-o
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      05-14-2008
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.

tom

--
THE DRUMMER FROM DEF LEPPARD'S ONLY GOT ONE ARM!
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      05-15-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.


Or spell "write."
 
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