Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Collections.sort()

 
MuraliH Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
can any one kindly explain what is the mistake I am doing in the following code. Why it is not compiling.

import java.util.*;
public class CollectionTest1 {
public static void main(String[] a)
{
Set<animal> s = new HashSet<animal>();
s.add(new animal("aa"));
s.add(new animal("ab"));
s.add(new animal("ac"));
s.add(new animal("ad"));

animal[] ar = (animal[])s.toArray();
Collections.sort(s);

}

}

class animal implements Comparable<animal>
{
String i;
animal(String i)
{
this.i = i;
}
public String getI()
{
return i;
}
public int compareTo(animal a1) {
return i.compareTo(a1.getI());
}
}

Thanks in advance.
 
Henry Wong
author
Marshal
Pie
Posts: 21409
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why it is not compiling.


It would help if you tell us the compiler error that you are getting.

Henry
 
Erica Barbato
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Collections.sort needs a List as parameter,but you are using a Set
[ January 20, 2008: Message edited by: erishine ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic