Question:
java collection.sort help---urgent?
Catherine
2012-08-14 16:44:45 UTC
I did the arraylist sort but on my eclipse it has an error, i dont know what happened? can anyone help me plz
public static void main(String[] args) {
ArrayList cAList = new ArrayList();
cAList.add(Color.red);
cAList.add(Color.orange);
cAList.add(Color.yellow);
cAList.add(Color.green);
cAList.add(Color.cyan);
cAList.add(Color.blue);
cAList.add(Color.magenta);
Collections.sort(cAList);
for(Color c : cAList)
System.out.println(c);
}
at the collection.sort(cAList) it has an error statement:
Multiple markers at this line
- Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (ArrayList). The
inferred type Color is not a valid substitute for the bounded parameter >
- Line breakpoint:A1 [line: 35] - main(String[])

help plz...
Four answers:
The Shadowman
2012-08-14 17:06:29 UTC
This forum should help you:

http://stackoverflow.com/questions/791429/java-sorting-object-in-arraylist

http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property



And this one is helpful:

http://www.albeesonline.com/blog/2008/10/16/sorting-an-arraylist-of-objects/
Ratchetr
2012-08-14 17:29:50 UTC
What are you expecting the output to be? Which color comes first in the list, and which is last?



Do you want them sorted alphabetically? So blue comes first and yellow is last? (What if I speak German and have different names for colors than you?)



Do you want them sorted by rainbow order? So red is first and blue is last?



Do you want them sorted by intensity? Not sure who would be first and last here.



Do you want them sorted from your most favorite to your least favorite color? No clue who would be first and last here either.



In order for something to be sortable in Java, it has to implement the Comparable interface. To implement Comparable, you have to write a function that compares 2 parameters to determine one is greater than or equal to the other.



So when they wrote the Color class in Java, and they thought about implementing Comparable, they probably asked the same questions I asked above. And they came to the conclusion that sorting colors is a quagmire. There is no 1 right way to do it. So they didn't implement Comparable. And if an object doesn't implement Comparable, it isn't sortable.



You will have to write your own implementation of Comparable and sort the colors in whatever way you think they should be sorted. Previous answer provided some links that should be useful if you decide you really need to do that.
?
2016-10-21 11:56:41 UTC
i guess you opt for to strengthen a JAVA IDE... someone already indexed out that lot of loose IDE's accessible and he's incredibly right - they're extraordinarily a lot complicated to get a draw close escially if you're on a good timeline. i visit point you that attempt sorting out an undemanding textual content editor on the internet first. So atleast you do not ought to waste time on arising textual content processing code like reproduction/paste, and so on. next aspect might want to be to characteristic yet another determination to the textual content editor for compiling the code. least confusing determination is to make a decision to the OS and get in touch with the javac alongside with the record call and site. next attempt giving some concepts with the assemble command. this way you are able to incrementally upload extra function. once you get the textual content editor to collect your code, atleast you are able to ' in theory' call it a Java IDE.
2014-11-06 21:13:08 UTC
confusing subject. browse on to yahoo and bing. this may help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...