This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

ArrayList Sorting Using Comparator? Exception at Runtime

 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am getting Exception at Runtime.
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soumya Ranjan Mohanty wrote:
I am getting Exception at Runtime.


Yea. I think you're gonna need to give us a bit more detail than that.

Henry
 
Himanshu Mall
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Please check your ObjComp constructor, the instance member "name" will contain null for all of the objects.
2) Provide a valid implementation of public String toString() if you want to print out the list elements. (Check out Arrays.toString() method as well. <It is really interesting>)



What the program would print now?

[Glen, Michael, Ricky, Shane, Shaun, Stuart]


Hope it helps.

Himanshu Mall
@mall.himanshu84@gmail.com
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your ObjComp class, and its field variable initializations......
 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you please modify my code and make it working. I want to sort the elements in the ArrayList Using the Collections.sort().
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soumya Ranjan Mohanty wrote:Would you please modify my code and make it working. I want to sort the elements in the ArrayList Using the Collections.sort().


Himanshu Mall explained it. Have a look on it.
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just modify your ObjComp class constructor as follows:



And override your toString() method, for instance:


Otherwise, Object class toString() will be called for your ObjComp class instances.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic