//第壹個類
public class Content {
private int key;
private int name;
//int、long、float、double、byte、short、String、StringBuffer
public Content(int key, int name) {
this.key = key;
this.name = name;
}
public Content(int key, long name) {
this.key = key;
this.name = (int)name;
}
public Content(int key, double name) {
this.key = key;
this.name = (int)name;
}
public Content(int key, byte name) {
this.key = key;
this.name = (int)name;
}
public Content(int key, short name) {
this.key = key;
this.name = (int)name;
}
public Content(int key, String name) {
this.key = key;
this.name = Integer.parseInt(name);
}
public long getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
}
//第二個類
import java.util.Comparator;
public class ContentComparator implements Comparator {
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Content c1 = (Content) o1;
Content c2 = (Content) o2;
if (c1.getName() > c2.getName()) {
return 1;
} else {
if (c1.getName() == c2.getName()) {
return 0;
} else {
return -1;
}
}
}
}
//第三個類
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class LinkNode {
public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
for (int i = 0 ; i < 10; i++){
int a = (int) (Math.random() * (100-1)+1);
list.add(new Content(i,a));
}
ContentComparator comp = new ContentComparator();
Collections.sort(list,comp);
Content content;
for(int i = 0; i < list.size(); i++){
content = (Content)list.get(i);
System.out.println(" Random " + content.getName());
}
}
}