Results 1 to 4 of 4
  1. #1
    Bit_Void is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Help With Sorting Array Please!

    I'm making a method to take an array of integers and rearrange the numbers from least to greatest, using for loops. I'm getting the error "java.lang.ArrayIndexOutOfBoundsException: 8," and I don't know why. What should I do to fix the code?

    Portion of the ArrayMethods class with the sorting method
    Java Code:
    public static Integer[] sortArray (Integer[] a)
      {
          int swap;
          
          for (int i = 0; i < a.length; i++)
          {
             
              for (int j = 1; j < a.length; j++)
              {
                  int x = j + 1;
                  if (a[j] > a[x])
                  {
                      swap = a[j];
                      a[j] = a[j+1];
                    a[j+1] = swap;
                      
                     
                  }
              }
              
          }
          return a;
          
      }
    Portion of the Driver that uses the code
    Java Code:
    import java.util.Arrays;
    public class ArrayMethodsDriver
    {
      public static void main(String[] args)
      {
        Integer[] a = {7,8,8,3,4,9,8,7};
        
        System.out.println(Arrays.toString(ArrayMethods.sortArray(a)));
      }
    }
    Upon running, if (a[j] > a[x]) is highlighted.

    Thanks a ton for any help.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Help With Sorting Array Please!

    Hi,
    Your loop with j goes from 1 to array length-1 and then you add 1 for variable x.
    So your x value goes from 2 to array length.
    But the elements in an array go from 0 to length-1 only, you cannot access an element length.
    E.g. an array with 3 elements has the elements 0,1 and 2.

    Konrad

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: Help With Sorting Array Please!

    Similarly, you are ignoring your 0th value in the sort.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help With Sorting Array Please!

    ... also, your sort method compares too many elements (so it is slower than it has to be; check your loops).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Code for Array and Sorting
    By Sync in forum New To Java
    Replies: 9
    Last Post: 10-30-2011, 03:22 AM
  2. Sorting Array UI
    By Brandon Seale in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 02:50 AM
  3. Help with sorting an object array.
    By TommyLR in forum New To Java
    Replies: 1
    Last Post: 02-03-2011, 12:43 PM
  4. Array List Sorting
    By makpandian in forum New To Java
    Replies: 5
    Last Post: 11-14-2010, 03:33 AM
  5. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 04:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •