Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparable interface. Doubt in ComparableTest class. Please find the question as comment below.

 
Ranjitha Serginaro
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* Pgm to display the student details after sorting them with respect to their GPA using the compareTo() method . . .*/





/*Output
Name = Dan Costello ID = 104 GPA = 2.3
Name = William Costigan ID = 102 GPA = 2.8
Name = Jamie Sullivan ID = 103 GPA = 3.6
Name = Mike Larry ID = 101 GPA = 4.0
BUILD SUCCESSFUL (total time: 0 seconds)
*/
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Ranjitha !

Ranjitha Serginaro wrote:
Object[] studentArray=studentSet.toArray(); /* Why is 'Object' class used here instead of 'Student' class to create the 'studentArray' object.
Also what does toArray() method do?*/

becasue java.util.AbstractCollection.toArray() returns Object[] . If you want to return Student[] then you need to consider generic version - T[] toArray(T[] a)
for more detail : go through JavaDoc.

and couple of suggestion :
1.dont use double for calculation, it may loose precision in result . So, consider java.lang.Double.compare(double, double)

2.use generic collection instead of error prone raw collection .

in this way you can do clean array conversion as below code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic