Results 1 to 20 of 37
- 10-16-2009, 06:59 PM #1
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.
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.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
So far I have changed the code but i am getting numerous errors
The errors areJava 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]);
As I say prior to adapting the code to a 2d array everything worked fine but now I am lost.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)
I would appreciate some advice and assistance.
Thanks in advance.
- 10-16-2009, 07:09 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Instead of Arrays.sort(twoDArray), do
for(oneDArray : twoDArray) {
Arrays.sort(oneDArray );
}
i.e only pass a one dimensional array to Arrays.sort.
- 10-16-2009, 07:13 PM #3
Just so I understand that is saying just to pass the relevant dimension to sort ??
- 10-16-2009, 07:20 PM #4
so if i understand correctly which i don't think i do
I should be entering
Java Code:for([0]:[1]) { Arrays.sort([1]); }
- 10-16-2009, 07:24 PM #5
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.only pass a one dimensional array to Arrays.sort
- 10-16-2009, 07:26 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 10-16-2009, 07:44 PM #7
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.
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 withfor(oneDArray : twoDArray) {
Arrays.sort(oneDArray );
}
I know this is not 100% as I have tried it but a yay or ney would be appreciated.Java Code:for([1] : [0]) { Arrays.sort(mbrnum);
- 10-16-2009, 07:47 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Better try it and find out if it works or not.
- 10-16-2009, 07:52 PM #9
I have tried it, believe me otherwise I would not be asking, like I say am I going in the right direction?
- 10-16-2009, 08:05 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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?
- 10-16-2009, 08:16 PM #11
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
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.for(oneDArray : twoDArray) {
Arrays.sort(oneDArray );
}
- 10-16-2009, 08:29 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Java Code:for (String[] array : mbrnum ) { Arrays.sort(array); }
- 10-16-2009, 08:41 PM #13
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.
this is saying to ignore the first array of strings in mbrnum and then sort the second array within a for loop?for (String[] array : mbrnum ) {
Arrays.sort(array);
}
- 10-16-2009, 08:46 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
No. It says sort all the arrays inside the mbrnum array.
read my previous responses again. A 2D array is an array of arrays.
- 10-16-2009, 08:51 PM #15
I replaced the original
WithJava Code:Arrays.sort(mbrnum);
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:for (String[] array : mbrnum ) { Arrays.sort(array); }
Thanks for your help so far.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)
- 10-16-2009, 08:54 PM #16
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Post your frmBubbleSort's line number 670.
- 10-16-2009, 08:57 PM #17
Line 670 is
Java Code:Arrays.sort(mbrnum);
- 10-16-2009, 08:59 PM #18
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
headDesk++ ;
- 10-16-2009, 08:59 PM #19
Sorry wrong 1, its
670 Arrays.sort(array);
- 10-16-2009, 09:03 PM #20
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Problem With Dimension of Jasper Report
By Prashant.surwade in forum Advanced JavaReplies: 3Last Post: 09-19-2009, 02:29 PM -
applet and dimension
By mensaFool in forum New To JavaReplies: 5Last Post: 09-15-2009, 04:37 AM -
[SOLVED] how to align JTextFiels[dimension] and JComboBox[dimension]
By Wolverine in forum AWT / SwingReplies: 5Last Post: 05-18-2009, 12:42 PM -
noob: two-dimension array
By bobmasta5 in forum New To JavaReplies: 5Last Post: 03-15-2009, 11:42 PM -
Arraylist to a 2- dimension array conversion
By mars123 in forum New To JavaReplies: 1Last Post: 12-06-2007, 11:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks