Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Ordering an array

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening everyone.

I have a question for you.

If I want to order a collection of objects which belong to the class "A", I have to use the method "sort".
If I want to use the method "sort", the class A has to implement the interface Comparable and define the method compareTo.

Does the same goes for the method sort of the arrays (in the case I want to create an array of objects)?
 
Marshal
Posts: 68904
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claud Mann wrote:. . . If I want to use the method "sort", the class A has to implement the interface Comparable . . .

Only if the parameter for sort() specifies Comparable.Have a look at the Java™ Tutorials, which explains object ordering much better than I would. Also see these two methods: 1 2. Method 1 was written before generics, so it can't be changed to support generics. You can pass an Object[] to it where Object doesn't implement Comparable and the compiler will be happy and you will suffer a class cast exception at runtime. Link 2 doesn't have these problems.
 
Claud Mann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...o it can't be changed to support generics. You can pass an Object[] to it where Object doesn't implement Comparable and the compiler will be happy and you will suffer a class cast exception at runtime. Link 2 doesn't have these problems.



thank you very much Campbell
 
Campbell Ritchie
Marshal
Posts: 68904
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure :)

It would be possible to add a method to Arrays and deprecate the sort(Object[]) method, which is still suffering from Java® being issued in 1996 without generics implemented.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic