level 7
cgxnjnj
楼主
package List;
import java.util.*;
public class fsdfsd {
public static void printElement(Collection c) {
Iterator it = c.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* ArrayList al = new ArrayList(); al.add(new Point(3, 3)); al.add(new
* Point(4, 4)); al.add(new Point(5, 5));
*
* System.out.println(al); printElement(al); Iterator it =
* al.iterator(); it.next(); // it.remove(); while (it.hasNext()) {
* System.out.println(it.next());
*
* }
*/
Student s1 = new Student(1, "zhangshan");
Student s2 = new Student(2, "lishi");
Student s3 = new Student(3, "wangwu");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al);
printElement(al);
}
}
class Student implements Comparable {
int num;
String name;
public Student(int num, String name) {
this.num = num;
this.name = name;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Student s = (Student) o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String tostString() {
return num + ":" + name;
}
}
2016年05月01日 06点05分
1
import java.util.*;
public class fsdfsd {
public static void printElement(Collection c) {
Iterator it = c.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* ArrayList al = new ArrayList(); al.add(new Point(3, 3)); al.add(new
* Point(4, 4)); al.add(new Point(5, 5));
*
* System.out.println(al); printElement(al); Iterator it =
* al.iterator(); it.next(); // it.remove(); while (it.hasNext()) {
* System.out.println(it.next());
*
* }
*/
Student s1 = new Student(1, "zhangshan");
Student s2 = new Student(2, "lishi");
Student s3 = new Student(3, "wangwu");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al);
printElement(al);
}
}
class Student implements Comparable {
int num;
String name;
public Student(int num, String name) {
this.num = num;
this.name = name;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Student s = (Student) o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String tostString() {
return num + ":" + name;
}
}