Page 1 of 2 12 LastLast
Results 1 to 20 of 37
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Arrays -changing from 1 dimension to 2d

    :confused:

    I had a 1 dimensional array using the array class() to search a list of strings which are numbers, everything works.

    Java Code:
    System.out.println("After Search");
               
                        String[] nums={"21.675", "24.798", "16.567", "25.014",
                        "32.907", "31.456", "41.876", "22.123", "20.945","20.679",
                        "18.398", "17.075", "40.025", "26.564", "30.712", "29.897",
                        "23.397", "35.548", "22.123", "31.398" }; 
    
    Arrays.sort(mbrnum);
                        txtResult.setText(mbrnum[0][1]); // sends 1st (lowest) number to textfield
                        // send number to textfield
                        System.out.println("Lowest Bmi Reading = " + mbrnum[0][1]);
                         //   System.out.println("Maximum = " + nums[nums.length-1]);
                        // Prints out variable nums which is highest number
    However now I have to change this so it searches that same set of numbers but in a 2d array, I have only just started to look at 2d arrays so would be grateful for some help.

    So far I have changed the code but i am getting numerous errors

    Java Code:
    String[][] mbrnum = new String [20][2]; // 2 dimensional array
    
    mbrnum[0][0]= "DAN100215";mbrnum[1][0]= "BUC200475";
    			    mbrnum[2][0]= "ADA505630";mbrnum[3][0]= "JOH300312";
    			    mbrnum[4][0]= "WHI776543";mbrnum[5][0]= "PEN134567";
    			    mbrnum[6][0]= "MUR455621";mbrnum[7][0]= "CRA097681";
    			    mbrnum[8][0]= "SIM0409152";mbrnum[9][0]= "MCC609863";
                    mbrnum[10][0] = "HAL609702";mbrnum[11][0] = "BUC408941";
                    mbrnum[12][0] = "NEW509039";mbrnum[13][0] = "PAR955542";
                    mbrnum[14][0] = "WALIIII999";mbrnum[15][0] = "THE479819";
                    mbrnum[16][0] = "LUC341287";mbrnum[17][0] = "WIL953216";
                    mbrnum[18][0] = "FAL200474";mbrnum[19][0] = "ABR970351";
    
                    mbrnum[0][1] = "21.675";mbrnum[1][1] = "24.798";
                    mbrnum[2][1]= "16.567";mbrnum[3][1] = "25.014";
                    mbrnum[4][1] = "32.907";mbrnum[5][1] = "31.456";
                    mbrnum[6][1] = "41.876";mbrnum[7][1] = "22.123";
                    mbrnum[8][1] = "20.945";mbrnum[9][1] = "20.679";
                    mbrnum[10][1] = "18.398";mbrnum[11][1] = "17.075";
                    mbrnum[12][1] = "40.025";mbrnum[13][1] = "26.564";
                    mbrnum[14][1] = "30.712";mbrnum[15][1] = "29.897";
                    mbrnum[16][1] = "23.397";mbrnum[17][1] = "35.548";
                    mbrnum[18][1] = "22.123";mbrnum[19][1] = "31.398";
    
                        Arrays.sort(mbrnum);
                        txtResult.setText(mbrnum[0][1]); // sends 1st (lowest) number to textfield
                        // send number to textfield
                        System.out.println("Lowest Bmi Reading = " + mbrnum[0][1]);
    The errors are

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable
            at java.util.Arrays.mergeSort(Unknown Source)
            at java.util.Arrays.mergeSort(Unknown Source)
            at java.util.Arrays.mergeSort(Unknown Source)
            at java.util.Arrays.sort(Unknown Source)
            at bubblesortarray.frmBubbleSort.rbtnSmallestBmiActionPerformed(frmBubbleSort.java:670)
            at bubblesortarray.frmBubbleSort.access$400(frmBubbleSort.java:26)
            at bubblesortarray.frmBubbleSort$5.actionPerformed(frmBubbleSort.java:260)
            at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
            at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
            at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
            at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
            at java.awt.Component.processMouseEvent(Unknown Source)
            at javax.swing.JComponent.processMouseEvent(Unknown Source)
            at java.awt.Component.processEvent(Unknown Source)
            at java.awt.Container.processEvent(Unknown Source)
            at java.awt.Component.dispatchEventImpl(Unknown Source)
            at java.awt.Container.dispatchEventImpl(Unknown Source)
            at java.awt.Component.dispatchEvent(Unknown Source)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
            at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
            at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
            at java.awt.Container.dispatchEventImpl(Unknown Source)
            at java.awt.Window.dispatchEventImpl(Unknown Source)
            at java.awt.Component.dispatchEvent(Unknown Source)
            at java.awt.EventQueue.dispatchEvent(Unknown Source)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.run(Unknown Source)
    BUILD SUCCESSFUL (total time: 7 seconds)
    As I say prior to adapting the code to a 2d array everything worked fine but now I am lost.

    I would appreciate some advice and assistance.

    Thanks in advance.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Instead of Arrays.sort(twoDArray), do

    for(oneDArray : twoDArray) {
    Arrays.sort(oneDArray );
    }
    i.e only pass a one dimensional array to Arrays.sort.

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Just so I understand that is saying just to pass the relevant dimension to sort ??

  4. #4
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    so if i understand correctly which i don't think i do

    I should be entering

    Java Code:
    for([0]:[1]) {
                    Arrays.sort([1]);   
                    }

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    only pass a one dimensional array to Arrays.sort
    So the arrays class will only deal with 1 dimensional arrays, I have tried to make sense of the API but it does not help.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read the API specs for the Arrays.sort method. It doesn't take any dimension. It takes an array of Comparables. Pass it a one dimensional array of strings instead of a two dimensional array. A 2D array is just an array of 1D arrays.

  7. #7
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    I have read the API prior to coming into the forum, I understand the fact the no matter how many dimensions the array has they are all classed as collections of arrays.

    The part that I am having trouble with is the couple of lines of code as above.

    for(oneDArray : twoDArray) {
    Arrays.sort(oneDArray );
    }
    It is the second array within the 2d array that I want to sort and search so going off your suggestion I asked if I was heading in right direction with

    Java Code:
    for([1] : [0]) {
                    Arrays.sort(mbrnum);
    I know this is not 100% as I have tried it but a yay or ney would be appreciated.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Better try it and find out if it works or not.

  9. #9
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    I have tried it, believe me otherwise I would not be asking, like I say am I going in the right direction?

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    mbrnum is a 2D array. Do not pass it to the sort method. It doesn't really make sense to try and sort a 2D array. You can however sort the 1D arrays that are inside the 2D array. So you can loop through each array in the 2D array and sort it using Arrays.sort.
    Does that make any sense?

  11. #11
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    It makes sense as far as not using the array that is not required for the search yes, but as far as the suggestion you made

    for(oneDArray : twoDArray) {
    Arrays.sort(oneDArray );
    }
    mbrnum is the 2d array so the strings making up the second array are what I want to search but the actual synax is what is baffling me.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Java Code:
    for (String[] array : mbrnum ) {
    	Arrays.sort(array);
    }

  13. #13
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    ok so if u dont mind rather than just taking and using, I want to understand the code.

    What I think it means and does.

    for (String[] array : mbrnum ) {
    Arrays.sort(array);
    }
    this is saying to ignore the first array of strings in mbrnum and then sort the second array within a for loop?

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    No. It says sort all the arrays inside the mbrnum array.
    read my previous responses again. A 2D array is an array of arrays.

  15. #15
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    I replaced the original
    Java Code:
    Arrays.sort(mbrnum);
    With

    Java Code:
    for (String[] array : mbrnum ) {
    Arrays.sort(array);
    }
    It compiles and runs but at the same point I get errors which can only be pointing to this section of code as everything else works fine.

    Java Code:
    After Search
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable
            at java.util.Arrays.mergeSort(Unknown Source)
            at java.util.Arrays.mergeSort(Unknown Source)
            at java.util.Arrays.mergeSort(Unknown Source)
            at java.util.Arrays.sort(Unknown Source)
            at bubblesortarray.frmBubbleSort.rbtnSmallestBmiActionPerformed(frmBubbleSort.java:670)
            at bubblesortarray.frmBubbleSort.access$400(frmBubbleSort.java:26)
            at bubblesortarray.frmBubbleSort$5.actionPerformed(frmBubbleSort.java:260)
            at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
            at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
            at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
            at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
            at java.awt.Component.processMouseEvent(Unknown Source)
            at javax.swing.JComponent.processMouseEvent(Unknown Source)
            at java.awt.Component.processEvent(Unknown Source)
            at java.awt.Container.processEvent(Unknown Source)
            at java.awt.Component.dispatchEventImpl(Unknown Source)
            at java.awt.Container.dispatchEventImpl(Unknown Source)
            at java.awt.Component.dispatchEvent(Unknown Source)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
            at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
            at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
            at java.awt.Container.dispatchEventImpl(Unknown Source)
            at java.awt.Window.dispatchEventImpl(Unknown Source)
            at java.awt.Component.dispatchEvent(Unknown Source)
            at java.awt.EventQueue.dispatchEvent(Unknown Source)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.run(Unknown Source)
    BUILD SUCCESSFUL (total time: 6 seconds)
    Thanks for your help so far.

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Post your frmBubbleSort's line number 670.

  17. #17
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Line 670 is

    Java Code:
    Arrays.sort(mbrnum);

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    headDesk++ ;

  19. #19
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Sorry wrong 1, its

    670 Arrays.sort(array);

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What does array contain? Post where you declared array.
    If array is 2D then you will get that exception. You won't get the exception if array is a String[].

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem With Dimension of Jasper Report
    By Prashant.surwade in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 03:29 PM
  2. applet and dimension
    By mensaFool in forum New To Java
    Replies: 5
    Last Post: 09-15-2009, 05:37 AM
  3. Replies: 5
    Last Post: 05-18-2009, 01:42 PM
  4. noob: two-dimension array
    By bobmasta5 in forum New To Java
    Replies: 5
    Last Post: 03-16-2009, 12:42 AM
  5. Arraylist to a 2- dimension array conversion
    By mars123 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 12:24 PM

Posting Permissions

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