I can NOT compile first class below I get the>>>.
//Chapter: 18-Generics, Problem - textbook:HTP7 JAVA DEITEL (c)2007, Exe.:18.4
C:\Users\User\Documents\TextBooksDataFiles\RexPractice\JAVA\MySolutions\18-4>javac SelectionSort.java
SelectionSort.java:53: incompatible types
found : T
required: T
T x = data[smallest];
^
SelectionSort.java:54: incompatible types
found : T
required: T
T y = data[index];
^
2 errors
I changed your code to compile. I did not test it. I also don't know whether it will do what you intended it to do. In answer to your original queston, you were defining T at the class level and T at the method level so you had two Ts defined. In your original code, try changing the method T to, for example S and and the compiler error will not be so confusing. BTW, You cannot use primitive types with generics.
exe instr: given an example in another chapter in selection sort[Searching/Sorting chapter], problem was use method sort with <T extends Comparable<T>>, and NOT class with this, to sort 10 numbers (in the original problem numbers in array random generated), well?
you say "try changing the method T to, for example S and and the compiler error will not be so confusing" how then bind T to S?
may wrtite this without in class the T but only in method: public < T extends Comparable< T >> void sort(T[] data) ???
BTW your code compiles and run correct.
To appear the contents of an obj array like Integer or Float I must use absolutelly: ENHANCED FOR LOOP correct?
Leonidas Savvides wrote:you say "try changing the method T to, for example S and and the compiler error will not be so confusing" how then bind T to S?
You cannot. T and S are different class types. I only suggested that to clear up confusion about the compiler error - not to remove the compiler error.
Leonidas Savvides wrote:To appear the contents of an obj array like Integer or Float I must use absolutelly: ENHANCED FOR LOOP correct?
Leonidas Savvides wrote:well since My code It compile/run success with below (is any error/improvement)?
can you comment on it (see code in prior post)...
I think they just wanted to show that you have choices in code design.
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
Gift giving made easy with the permaculture playing cards