Results 1 to 5 of 5
Thread: Whats wrong with this code???
- 03-28-2010, 02:08 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 49
- Rep Power
- 0
Whats wrong with this code???
Whats wrong ,, please help ??:confused::confused:Java Code:public class Try { public static void main(String[] args) { //create an integer array Integer[] intArray = {new Integer(2), new Integer (4),new Integer(4), new Integer (3)}; sort(intArray); printList(intArray); } public static void sort(Comparable list[]){ Comparable currentMax; int currentMaxIndex; for(int i = 0; i < list.length -1; i++){ for (int j = 0; j <list.length; j++){ if(list[i].compareTo(list[j]) < 0){ Comparable temp = list[i]; list[i] = list[j]; list[j] = temp; } } } } public static void printList(Object[] list){ for(int i = 0; i <list.length; i++){ System.out.print(list[i] +" "); System.out.println(); } } }
Moderator Edit: Code tags correctedLast edited by Fubarable; 03-28-2010 at 02:24 PM. Reason: [color="blue"]Moderator Edit: Code tags corrected[/color]
- 03-28-2010, 02:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2010, 02:15 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 49
- Rep Power
- 0
It doesnt sort them right !!
It works but sorts wrong !!
- 03-28-2010, 02:25 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2010, 05:46 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
It looks like you copy-and-pasted your sort() method from somewhere, and that you don't understand it. How do I know that?
1. Your sort() method wants a list of a type that's not really a type at all, but an Interface.
2. Your sort() method would be much more convenient if it called for a list of the same type that your list really is.
3. Your sort() method declares two local variables right away, and then never uses them.
You should start from the beginning again. Take out a deck of cards, and pull out just the ace through 10 of all four suits. Shuffle them, and deal out a row of ten cards on a table, face up. Sort them according to these rules:
1. You may pick up only one card at a time, and only with your right hand.
2. You may only lay a card down into one of the original ten spaces.
3. You may temporarily put one card in your left hand and hold it there while you pick up and lay down another card with your right hand.
Think about what you are doing while you sort them. Write down each step. Shuffle all forty cards again and deal out ten more. Sort them again, carefully following the instructions you wrote down last time (this is your algorithm). Did it work? Keep doing this with real, physical cards until you have an algorithm that you understand thoroughly and that works correctly.
Now you are ready to write your code. Your array is the ten spaces on the table. Your right hand is the index of your loop. Your left hand is a temporary variable.
Show us what you come up with.
-Gary-
Similar Threads
-
Database help... whats wrong?
By neosnokia in forum JDBCReplies: 4Last Post: 06-09-2009, 11:17 PM -
Whats wrong with this code?
By bbtgirl in forum New To JavaReplies: 2Last Post: 02-25-2009, 03:51 AM -
Whats wrong with my maths???
By soc86 in forum New To JavaReplies: 4Last Post: 11-03-2008, 05:52 PM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM -
Whats wrong with my code???
By Soda in forum New To JavaReplies: 2Last Post: 12-06-2007, 12:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks