【JavaSE】public int compare(Student o1, Student o2){...}Q:o1->s2 ,o2 -> s1

Q== this.name 与o.name ==
A1:第一个地址是调用这个方法的对象的地址;另一个地址是这个方法中参数传递过来对象的地址。
A2:this表示的是当前的节点对象,当参数传进去后,此对象传到函数中就变成名为obj的对象了(JS)

Test1:

M:public int compare(Student o1, Student o2){...}
W:o1->s2 ,o2 -> s1
package ch007collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * program@: day16_map
 * description@:
 *     需求:
 *         ArrayList存储学生对象,使用Collections对ArrayList进行排序
 *         要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
 */

public class CollectionsDemo02 {
    public static void main(String[] args) {
        //创建ArrayList集合
        ArrayList<Student> arrayList = new ArrayList<Student>();
        //创建Student对象
        Student s1 = new Student("林青霞",18);
        Student s2 = new Student("王祖贤",18);
        Student s3 = new Student("张曼玉",20);
        //add到ArrayList
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);

        //使用Collections对ArrayList排序
        // public static <T extends Comparable<? super T>> void sort(List<T> list)
        //匿名内部类的方式
        Collections.sort(arrayList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //按照年龄从小到大的顺序,年龄相同时,按照姓名的字母顺序排序
/*                System.out.println(o1);
                System.out.println("------");*/
                //debug01:o1->s2 o2 -> s1
                int num = o1.getAge() - o2.getAge();
                int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
                return num2;
            }
        });

        //遍历
 /*       for (Student s : arrayList){
            System.out.println(s);
        }
*/
//        arrayList.forEach(System.out::println);
        arrayList.forEach(x -> System.out.println(x));

    }
}

Test2:

W: debug01: o1 -> 3, o2 -> 1
package ch00;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * program@: Day16_Map
 * description@:
 */

public class CompareDemo {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(3);
        list.add(1);
        list.add(5);
        list.add(2);
        list.add(0);
        list.add(6);
        list.add(7);

        //下面是升序排列的
        Comparator<Integer> comparator1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (o1 > o2) {
                    return 1; //返回1表示o1权重大于o2权重,故o1排在o2后面,即升序
                } else if (o1 < o2) {
                    return -1; //返回-1表示o1权重小于o2权重,故o2排在o1后面,即升序
                }
                return 0;
            }
        };

        //下面是降序排列的
        Comparator<Integer> comparator2 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (o1 > o2) {
                    return -1; //返回-1表示o1权重小于o2权重,故o2排在o1后面,即降序
                } else if (o1 < o2) {
                    return 1; //返回1表示o1权重大于o2权重,故o1排在o2后面,即降序
                }
                return 0;
            }
        };
        Collections.sort(list, comparator1);
        System.out.println(list);
    }

}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值