Java Arrays.sort throws exception

Discussion in 'Java' started by captain, May 14, 2008.

  1. captain

    captain Guest

    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
    captain, May 14, 2008
    #1
    1. Advertising

  2. captain

    Roedy Green Guest

    On Wed, 14 May 2008 07:37:00 -0700 (PDT), captain
    <> wrote, quoted or indirectly quoted someone who
    said :

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

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, May 14, 2008
    #2
    1. Advertising

  3. captain

    Mark Space Guest

    captain 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


    Yeah, it's line 115 of your code. Just fix that up and you'll be ok.
    Mark Space, May 14, 2008
    #3
  4. captain

    captain Guest

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

    On May 14, 12:48 pm, Mark Space <> wrote:
    > captain 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

    >
    > Yeah, it's line 115 of your code.  Just fix that up and you'll be ok.
    captain, May 14, 2008
    #4
  5. captain <> writes:

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


    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, May 14, 2008
    #5
  6. captain wrote:
    > I am brand new here and don't mean to be flippant, but is this a joke?

    If you don't post your code nobody can help you.
    Andrea Francia, May 14, 2008
    #6
  7. captain

    captain Guest

    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

    On May 14, 11:59 am, Eric Sosman <> wrote:
    > captain 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?

    >
    >      It is conceivable that the sort() implementation has a
    > bug.  But it is far, Far, FAR more probable that the bug is
    > in the code you haven't shown ...
    >
    > --
    >
    captain, May 14, 2008
    #7
  8. captain

    captain Guest

    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




    On May 14, 11:15 am, Roedy Green <>
    wrote:
    > On Wed, 14 May 2008 07:37:00 -0700 (PDT), captain
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    > >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 athttp://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.
    >
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com
    captain, May 14, 2008
    #8
  9. captain

    Mark Space Guest

    Lasse Reichstein Nielsen wrote:
    > captain <> writes:
    >
    >>> Yeah, it's line 115 of your code. Just fix that up and you'll be ok.

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


    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.
    Mark Space, May 14, 2008
    #9
  10. captain

    captain Guest

    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:18 pm, Lasse Reichstein Nielsen <> wrote:
    > captain <> writes:
    > >> Yeah, it's line 115 of your code.  Just fix that up and you'll be ok.

    > > 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
    > --
    > Lasse Reichstein Nielsen  -  
    >  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    >   'Faith without judgement merely degrades the spirit divine.'
    captain, May 14, 2008
    #10
  11. captain

    captain Guest

    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 <> 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.
    >
    > --
    >
    captain, May 14, 2008
    #11
  12. 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
    Andrea Francia, May 14, 2008
    #12
  13. 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
    Patricia Shanahan, May 14, 2008
    #13
  14. captain

    Roedy Green Guest

    On Wed, 14 May 2008 11:32:07 -0700 (PDT), captain
    <> 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
    Roedy Green, May 14, 2008
    #14
  15. captain

    Roedy Green Guest

    On Wed, 14 May 2008 20:00:05 GMT, Andrea Francia
    <-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
    Roedy Green, May 14, 2008
    #15
  16. captain

    Roedy Green Guest

    On Wed, 14 May 2008 12:38:11 -0700 (PDT), captain
    <> 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
    Roedy Green, May 14, 2008
    #16
  17. captain

    Roedy Green Guest

    On Wed, 14 May 2008 12:36:06 -0700 (PDT), captain
    <> 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
    Roedy Green, May 14, 2008
    #17
  18. On May 14, 10:37 am, captain <> 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
    Owen Jacobson, May 14, 2008
    #18
  19. captain

    Tom Anderson Guest

    On Wed, 14 May 2008, Owen Jacobson wrote:

    > On May 14, 10:37 am, captain <> 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!
    Tom Anderson, May 14, 2008
    #19
  20. captain

    Mark Space Guest

    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."
    Mark Space, May 15, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Miller
    Replies:
    4
    Views:
    13,289
    Chris Smith
    Nov 22, 2003
  2. sravan
    Replies:
    2
    Views:
    1,337
    Alan Moore
    Sep 3, 2004
  3. Replies:
    2
    Views:
    327
  4. Philipp
    Replies:
    21
    Views:
    1,111
    Philipp
    Jan 20, 2009
  5. Navin
    Replies:
    1
    Views:
    674
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page