• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

I can NOT compile first class below

 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code It compile/run success with below (is any error)?
can you comment on it (see and prior post)...

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

No. See this recent post: https://coderanch.com/t/500383/java/java/operator-works-Integer-class
 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well since My code It compile/run success with below (is any error/improvement)?
can you comment on it (see code in prior post)...
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic