Java Arrays.sort throws exception

C

captain

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
 
R

Roedy Green

When the number of elements gets above around 2700 the Arrays.sort
inside the Collections.sort throws an exception:

java.lang.ArrayIndexOutOfBoundsException

We will have to see your code. In the meantime have a look at
http://mindprod.com/jgloss/sort.html
to learn how to write sorts.

Presumably your Comparator is doing some indexing which is failing.
Look carefully at the stack trace for the precise line that is
failing.
 
M

Mark Space

captain said:
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

Yeah, it's line 115 of your code. Just fix that up and you'll be ok.
 
L

Lasse Reichstein Nielsen

I am brand new here and don't mean to be flippant, but is this a joke?

Yes, it's a joke, with a point. The point being that we can't find the
problem if you don't show us code that exhibits the problem.

In the unlikely event that Arrays.sort had a bug that showed already
at 2700 elements, and that we knew of it, we could, in theory, guess
that it was that bug you were hitting.

As someone else said, the probability of the bug being in the standard
library vs. the probability of it being in your code suggests that you
should just go ahead and show your code ... or rather show a nicely
reduced example without unnecessary distractions but which still
exhibits the problem.

Often, the act of cutting down to a SSCCE (Small Self-Contained Compilable
Example) will reveal the bug along the way.

Also remember to note which JVM version you are running on. If the bug
is related to the standard library, then that might also be important.

In general, when you need help with a programming problem, you should
provide enough information for us to do the "Three R's". We need to be able
to:
Reproduce the problem: Say what you do. Be precise (give a SSCCE
that exhibits the problem). Further details are good.
Recognize the problem: Say what happens that you consider as the problem.
It might be expected behavior. It might not happen to to someone with
a different configuration.
Repair the problem: Say what you expected to happen. Can't fix the
problem if we don't know what to fix it *to*.

/L
 
C

captain

Eric,

Thank you very much for your response.

My code:

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]);
}
}

Thanks,

Fred
 
C

captain

Roedy,

Thank you very much for your response.

My code:

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]);
}
}

Thanks,

Fred
 
M

Mark Space

Lasse said:
Yes, it's a joke, with a point. The point being that we can't find the
problem if you don't show us code that exhibits the problem.

Pretty much. And a cursor search of this news group for various "help"
topics always yields the same replies:

1. Post an SSCCE
2. Post an SSCCE
3. Post an SSCCE

Assuming you did a search first to try to solve your problem, you should
have hit these. So I can only conclude that you did very little
research of your own before posting your message. And it's getting
irritating to keep making the exact same reply to each of these requests.

Post an SSCCE.
 
C

captain

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
 
C

captain

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

captain said:
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.
 
A

Andrea Francia

captain said:
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
 
P

Patricia Shanahan

Andrea said:
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
 
R

Roedy Green

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

Roedy Green

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

Roedy Green

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

Owen Jacobson

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
 
T

Tom Anderson

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
 
M

Mark Space

Eric said:
captain said:
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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top