请帮我实现这2个方法,谢谢。要求代码越少越好,可以另外再添加类或方法/**
 *根据id将Ms们进行排序(id由大到小)
 */
public List orderbyid(List list);/**
 *每个国家各选一个id最高的Ms,放到list中,再排序(按id由大到小)返回
 */
public List getMaxIdOfEveryCountry(List list);

------具体代码如下--------------Class Ms{
long id;
String country;//国家
long age;//年龄
long temperament;//气质
long beauty;//美丽public Ms(long id,String country,long age,long temperament,long beauty){
this.id=id;
this.country=country;//国家
this.age=age;//年龄
this.temperament=temperament;//气质
this.beauty=beauty;//美丽
}}Interface Select{ // 选美
/**
 *根据id将Ms们进行排序(id由大到小)
 */
public List orderbyid(List list);/**
 *每个国家各选一个id最高的Ms,放到list中,再排序(按id由大到小)返回
 */
public List getMaxIdOfEveryCountry(List list);}Class Judge implements Select{//评委
/**
 *根据id将Ms们进行排序(id由大到小)
 */
public List orderbyid(List list){
//这边怎么实现啊?代码越少越好。
}
/**
 *根据id将Ms们进行排序(id由大到小)
 */
public List orderbyid(List list){
//这边怎么实现啊?代码越少越好。
}
public static void main(String[] args){
Ms c1=new Ms(1,"China",23,100,100);
Ms c2=new Ms(2,"China",21,100,100);
Ms s1=new Ms(3,"Singapore",23,100,100);
Ms i1=new Ms(4,"Iran",24,100,100);
Ms i2=new Ms(5,"Iran",24,100,100);
Ms s2=new Ms(6,"Singapore",24,100,100);
Ms[] mses=new Ms[](c1,c2,s1,s2,i1,i2);
//toArray;Judge j=new Judge();
//测试
Judge.orderbyid(list);
Judge.getMaxIdOfEveryCountry(list);}}

解决方案 »

  1.   

    Arrays.sort(数组,

    Collections.sort(列表,
      new Comparator<Ms>(){
        int compare(Ms a, Ms b){
          return a.getId()-b.getId();
        }
      }
    )
    正确的怎么写忘了,楼主搜一下吧
      

  2.   

    让你的Ms类实现Compareable,然后增加方法如下
    public int compareTo(Object o)
    {
    Ms ms=(Ms)o;
    int result=0;
    if(ms.id>this.id)
    {
    result=-1;
    }
    else if(ms.id<this.id)
    {
    result=1;
    }
    return result;
    }
    然后你要实现的方法代码如下:
    Collections.sort(list);
    两个方法都一样,返回值不用list,用void一样的,一定要返回的话返回list这是从小到大排列的,如果要从大到小的话,
    让你的Ms类实现Compareable,然后增加方法如下
    public int compareTo(Object o)这个方法的1换成-1,-1换成1就行了
      

  3.   

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    class Ms {
    long id;
    String country;// 国家
    long age;// 年龄
    long temperament;// 气质
    long beauty;// 美丽 public Ms(long id, String country, long age, long temperament, long beauty) {
    this.id = id;
    this.country = country;// 国家
    this.age = age;// 年龄
    this.temperament = temperament;// 气质
    this.beauty = beauty;// 美丽
    } /* public int compareTo(Object o) {
     return (int) (((Ms) o).id - this.id);
     }*/
    }interface Select { // 选美
    /**
     * 根据id将Ms们进行排序(id由大到小)
     */
    public List orderbyid(List list); /**
     * 每个国家各选一个id最高的Ms,放到list中,再排序(按id由大到小)返回
     */
    public List getMaxIdOfEveryCountry(List list);}class Judge implements Select {// 评委 public List getMaxIdOfEveryCountry(List list) {
    Comparator comp = new MycomparatorMs();
    Collections.sort(list, comp);
    Map m = new HashMap();
    List L = new ArrayList();
    for (int i = 0; i < list.size(); i++) {
    if (!m.containsKey(((Ms) list.get(i)).country)) {
    L.add((Ms) list.get(i));
    m.put(((Ms) list.get(i)).country, list.get(i));
    }
    }
    return L;
    } public List orderbyid(List list) {
    Comparator comp = new MycomparatorMs();
    Collections.sort(list, comp);
    return list;
    } public static void main(String[] args) {
    Ms c1 = new Ms(1, "China", 23, 100, 100);
    Ms c2 = new Ms(2, "China", 21, 100, 100);
    Ms s1 = new Ms(3, "Singapore", 23, 100, 100);
    Ms i1 = new Ms(4, "Iran", 24, 100, 100);
    Ms i2 = new Ms(5, "Iran", 24, 100, 100);
    Ms s2 = new Ms(6, "Singapore", 24, 100, 100);
    Ms[] mses = new Ms[] { c1, c2, s1, s2, i1, i2 };
    // toArray;
    List list = new ArrayList();
    for (int i = 0; i < mses.length; i++) {
    Ms mm = mses[i];
    list.add(mm);
    }
    Judge j = new Judge();
    // 测试
    List list1 = j.orderbyid(list);
    for (int i = 0; i < list1.size(); i++) {
    Ms ms = (Ms) list1.get(i);
    System.out.println(ms.id + " " + ms.country);
    }
    System.out.println();
    List list2 = j.getMaxIdOfEveryCountry(list);
    for (int i = 0; i < list2.size(); i++) {
    Ms ms = (Ms) list2.get(i);
    System.out.println(ms.id + " " + ms.country);
    }
    }
    }
     class MycomparatorMs implements Comparator { public int compare(Object o1, Object o2) {
    Ms p1 = (Ms) o1;
    Ms p2 = (Ms) o2;
    if (p1.id < p2.id)
    return 1;
    else
    return 0;
    }}
      

  4.   

    三楼很勤奋
    补充一下,直接用减法不安全。可能出现NaN