Forums Register Login

ArrayList Sorting Using Comparator? Exception at Runtime

+Pie Number of slices to send: Send


I am getting Exception at Runtime.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Check your ObjComp class, and its field variable initializations......
+Pie Number of slices to send: Send
Would you please modify my code and make it working. I want to sort the elements in the ArrayList Using the Collections.sort().
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
grapes are vegan food pellets. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1677 times.
Similar Threads
Sorting Collection
problem with cast
JFrame confusion
Generics
Generics adding to List (? extends Object)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:57:44.