Forums Register Login

[SOLVED] Sorting an ArrayList

+Pie Number of slices to send: Send
Hey, guys. I have a homework assignment due later tonight and am having a bit of trouble. I have to sort an array list as part of the assignment. My understanding is that must use Collections.sort, but I can't get that to work. I am not sure if that is due to my use of generics or what. Here is my current code.



and the error messages I am getting are:



I've looked at quite a few explanations of sorting arraylists, and they all achieve sorting with Collections.sort( ArrayList );, but this isn't working for me. They all used string values for sorting and I am using numbers, but it seems like Collections.sort would be able to sort values of the Number type. Any help would be great. Let me know if I left out something you need to know.
+Pie Number of slices to send: Send
If you look at the JavaDoc for the Collections class, you will see that the sort() method that takes one parameter, takes a generic that is an upper bound of Comparable. The generic that you defined doesn't fulfill that requirement.

You have a few options... (1) you can create a new list that takes a comparable generic, and copy your elements over, (2) you can add an upper bound to your class to require that the type be comparable, or (3) you can use a comparator instead, as that sort method doesn't have that requirement.

The easiest of these options, is option 2... which means that you have to change your class as follows...



Henry
+Pie Number of slices to send: Send
And further down your code, where you create a MyList<Number>.... Well, you can't do that. The Number class doesn't implement Comparable.

Henry
+Pie Number of slices to send: Send
Ah, thank you. That seems to have worked. Believe it or not, I did look at the javadoc; it's just that I don't really know how to read the documentation, or how to learn how to read and understand it. I saw



and thought I just inserted a list (I assumed arraylist would work because I saw it in other peoples' code) of type T. Now that I've read your post I notice the



beside it, but I didn't know what that meant, nor that I could do <T extends Number & Comparable<? super T>>.

I see that if you click "sort" it takes you to a more detailed explanation, but when I looked at it earlier I saw



and didn't understand that I could do the same thing to a class, where as here it was a method.

Thanks for you help. I think that fixes it.
+Pie Number of slices to send: Send
For resources on generics, there were a few good links in this thread, particularly the Angelika Langer site:

https://coderanch.com/t/480743/Beginning-Java/java/About-Generics#2158324

+Pie Number of slices to send: Send
Ah, great. I will take a look. Thank you very much for your help.
Won't you please? Please won't you be my neighbor? - Fred Rogers. 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 6593 times.
Similar Threads
Need help finding assignments
warning: [unchecked] unchecked method invocation
Comparator vs comparable
Sorting Arrays
Collection sort
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:50:10.