Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sorting 2D Array problem

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Sorting 2D Array problem

    Hi, I am using a bubble sort algorithm to sort a 2D array. The array is as follows:

    String data[][] = {{"John", "Smith", "14.75", "30", "ZZZZ", "ZZZZ", "ZZZZ",},{"Bill", "Gates", "18.50", "40","ZZZZ", "ZZZZ", "ZZZZ",}, 
                    {"Steve","Jobs", "10.75", "25","ZZZZ", "ZZZZ", "ZZZZ",},{"Paul", "Allen","21.50","34","ZZZZ", "ZZZZ", "ZZZZ",},
                    {"Steve", "Ballmer", "18.75", "35","ZZZZ", "ZZZZ", "ZZZZ",},{"ZZZZ", "ZZZZ", "ZZZZ", "ZZZZ"}};    
     //ZZZZ are marker values,the last row is to hold total values of all employees

    I want to sort this 2D array by comparing last names (index 1), and bubble sorting all their corresponding values along with it.

    static public String[][] sort(String data[][])
        {
            for(int x = 0; x <= data.length-1; x++)
            {
                for(int y = 0; y<=data.length-2;y++)
                {
                    if(data[y][1].compareTo(data[y+1][1]) > 0)   //compare lastnames alphabetically
                    {
                        String temp = data[y][1];    //swap last names
                        data[y][1]=data[y+1][1];
                        data[y+1][1] = temp;
     
                        String temp2 = data[y][0];  //swap first names
                        data[y][0]=data[y+1][0];
                        data[y+1][0] = temp2;
     
                        String temp3 = data[y][2];  //etc
                        data[y][2]=data[y+1][2];
                        data[y+1][2] = temp3;
     
                        String temp4 = data[y][3];
                        data[y][3]=data[y+1][3];
                        data[y+1][3] = temp4;
     
                        String temp5 = data[y][4];
                        data[y][4]=data[y+1][4];
                        data[y+1][4] = temp5;
     
                        String temp6 = data[y][5];
                        data[y][5]=data[y+1][5];
                        data[y+1][5] = temp6;
     
                    }
                }   
     
     
            }

    It compiles but when I run it i get the error:
    java.lang.ArrayIndexOutOfBoundsException: 4


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Sorting 2D Array problem

    .ArrayIndexOutOfBoundsException
    Array indexes range from 0 the the array length-1
    Check the code to see which index is going past the end of the array.
    If you can't see, add some println statements that print out the values of the indexes. The print out will show you what index is too big.

    If you had copied the full text of the error message it would show the line number of the error and the value of the index.

    Hint: the second dim can be handled as a single dim array
    int[][] twoDim = {{1,2,3}, {4,5,6}};
    //  swap the one dim array parts
    int[] oneDimTemp = twoDim[0];  //  save
    twoDim[0] = twoDim[1];  // move
    twoDim[1] = oneDimTemp;
    System.out.println(Arrays.deepToString(twoDim));// [[4, 5, 6], [1, 2, 3]]
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    lordofrandom (May 4th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Sorting 2D Array problem

    Quote Originally Posted by Norm View Post
    Array indexes range from 0 the the array length-1
    Check the code to see which index is going past the end of the array.
    If you can't see, add some println statements that print out the values of the indexes. The print out will show you what index is too big.

    If you had copied the full text of the error message it would show the line number of the error and the value of the index.

    Hint: the second dim can be handled as a single dim array
    int[][] twoDim = {{1,2,3}, {4,5,6}};
    //  swap the one dim array parts
    int[] oneDimTemp = twoDim[0];  //  save
    twoDim[0] = twoDim[1];  // move
    twoDim[1] = oneDimTemp;
    System.out.println(Arrays.deepToString(twoDim));// [[4, 5, 6], [1, 2, 3]]
    Thanks a lot, sir.

Similar Threads

  1. Beginner Problem: Array sorting decreasing element values? Why?
    By Hatsman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 13th, 2013, 02:37 PM
  2. help in sorting the array
    By alloka in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 15th, 2012, 01:54 PM
  3. Array sorting
    By renars in forum Java Theory & Questions
    Replies: 4
    Last Post: May 17th, 2011, 10:45 AM
  4. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  5. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM