[Logo]
Forums Register Login
ArrayList Sorting Using Comparator? Exception at Runtime


I am getting Exception at Runtime.
 
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
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
Check your ObjComp class, and its field variable initializations......
Would you please modify my code and make it working. I want to sort the elements in the ArrayList Using the Collections.sort().
 
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.
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.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Dec 16, 2017 08:11:27.