2014年5月15日星期四

Newbie questions ! ! Join the List collection of objects , using a comparator to sort the contents of the object . Comparators do not realize how !

class Student implements Comparable {
private String name;
private int age;
private float score;
public Student (String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName () {
return name;
}
public void setName () {
this.name = name;
}
public int getAge () {
return age;
}
public void setAge () {
this.age = age;
}
public float getScore () {
return score;
}
public void setScore () {
this.score = score;
}

public String toString () {
return " Student Name ->" + this.name + ", the student's age ->" + this.age + ", student achievement -> "+ this.score;
}
public int compareTo (Student stu) {
if (this.score> stu.score) {
return 1;
} else if (this.score return -1;
} else {
return 0;
}
}
}
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class chapters1301 {
public static void main (String agrs []) {
List allList = new ArrayList ();
allList.add (new Student (" Joe Smith " , 21,84 ) ) ;
allList.add (new Student (" John Doe " , 23,80 ) ) ;
allList.add (new Student (" King of Five" , 22,89 ) ) ;
allList.add (new Student (" Sun Qi " , 19,100 ) ) ;
allList.add (new Student (" Zhao girl " , 20,75 ) ) ;
Iterator iter = allList.iterator ();
while (iter.hasNext ()) {
System.out.println (iter.next ());
}
System.out.println (allList);
}
}
------ Solution ----------------------------------- ---------
List is not so sort of
Collections.sort (list, Comparable)
------ Solution ---------------------------- ----------------
landlord of this comparison is the entity class itself implement the Comparable interface , also known as internal comparator , List is an ordered collection does not call the internal comparator to sort . Either you do not ArrayList, use TreeSet you want to sort List can use an external comparator

Collections.sort(list,new Comparator<Student>(){
    public int compare(Student s1,Student s2){//自己重写和你的Comparable的compareTo方法一样
    }
    public boolean equals(Student s){}
})

------ Solution ------------------------------------- -------
Iterator just used an iterative , not to sort .
you here to sort by :
Collections.sort(list)

------ For reference only ----------------------------------- ----
public class chapters1301 {
public static void main (String agrs []) {
List allList = new ArrayList ();
Collections.addAll (allList, new Student (" Joe Smith " , 21,84 ) ) ;
Collections.addAll (allList, new Student (" John Doe " , 23,80 ) ) ;
Collections.addAll (allList, new Student (" King of Five" , 22,89 ) ) ;
Collections.addAll (allList, new Student (" Sun Qi " , 19,100 ) ) ;
Collections.addAll (allList, new Student (" Zhao girl " , 20,75 ) ) ;
Collections.sort (allList);
Iterator iter = allList.iterator ();
while (iter.hasNext ()) {
System.out.println (iter.next () + ",");
}
}
}
That's it ! ! Haha Thank
------ For reference only ------------------------------------ ---

Well, this can also be achieved with a TreeSet ! ! Thank

没有评论:

发表评论