This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList Sorting Using Comparator? Exception at Runtime

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


I am getting Exception at Runtime.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic