but each time the compiler error , compareTo it wrong , as shown :
------ Solution ------- -------------------------------------
sort (intArray);
sort (doubleArray);
sort (charArray);
sort (stringArray);
these places with Arrays.sort ();
Arrays.sort (intArray);
------ For reference only --------------------------- ------------
to facilitate you Daniel, I put the code to stick around :
public class GenericSort {
public static void main (String [] args) {
Integer [] intArray = {new Integer (2), new Integer (4), new Integer (3)};
Double [] doubleArray = {new Double (3.4), new Double (1.3), new Double (-22.1)};
Character [] charArray = {new Character ('a'), new Character ('j'), new Character ('r')};
String [] stringArray = {"Tom", "John", "Fred"};
sort (intArray);
sort (doubleArray);
sort (charArray);
sort (stringArray);
System.out.println ("Sorted Integer objects:");
printList (intArray);
System.out.println ("Sorted Double objects:");
printList (doubleArray);
System.out.println ("Sorted Character objects:");
printList (charArray);
System.out.println ("Sorted String objects:");
printList (stringArray);
}
public static void sort (Object [] list) {
Object currentMax;
int currentMaxIndex;
currentMaxIndex = 0;
for (int i = list.length - 1; i> = 1; i -) {
currentMax = list [i];
currentMax = i;
for (int j = i - 1; j> = 0; j -) {
if (((Comparable) currentMax). compareTo (list [j]) <0) {
currentMax = list [j];
currentMaxIndex = j;
}
}
if (currentMaxIndex! = i) {
list [currentMaxIndex] = list [i];
list [i] = currentMax;
}
}
}
public static void printList (Object [] list) {
for (int i = 0; i
System.out.println ();
}
}
------ For reference only ---------------------------------- -----
uh. . sort is based on the following method to write sort of . Arrays are not in the class .
------ For reference only -------------------------------------- -
investigation API.
ClassCastException - if the specified object type does not allow it to compare with this object
Double and Integer objects of different types ( can not be strong turn )
------ For reference only ------------------ ---------------------
uh ... sorry, I was a new rookie, do not understand the problem you say .
I want to know , how should I change this procedure? Why call the source directly in the compareTo method not?
------ For reference only -------------------------------------- -
You say the "Double and Integer objects of different types ( can not be strong turn ) ," but just the sort method in its own class Integer comparison , Double class of their own comparisons. No boast class comparison.
------ For reference only -------------------------------------- -
uh ... sorry, I was a new rookie, do not understand the problem you say .
I want to know , how should I change this procedure? Why call the source directly in the compareTo method not?
Brother, you are so kind , I was a rookie .
can be used, I mean sort (doubleArray);
or give you cut a figure it
You see, one is the Double object , one Integer object , they can not switch between two strong , it will be reported this anomaly
------ For reference only ------- --------------------------------
uh ... sorry, I was a new rookie, do not understand the problem you say .
I want to know , how should I change this procedure? Why call the source directly in the compareTo method not?
Brother, you are so kind , I was a rookie .
can be used, I mean sort (doubleArray);
or give you cut a figure it
You see, one is the Double object , one Integer object , they can not switch between two strong , so this exception will be reported
I think I understand what you mean. Say the first sort (intArray); executed, started sort (doubleArray); while , currentMax is Integer type, but list [i] is the Double type. So there is an error , is this mean?
The first line of the Object currentMax sort method ; this will not be initialized on currentMax clear is not it ? If this is the reason , then , how can I change ?
------ For reference only -------------------------------------- -
I guess you have the wrong
currentMax = list [i];
currentMaxIndex = i;
see red word here is not wrong
------ For reference only ----------------------- ----------------
changed so you can run
------ For reference only correct ---------- -----------------------------
Blessings Yes, I careless , is wrong.
thanks! ! ! Thank you ! !
没有评论:
发表评论