|
Ctril+Alt+L IDEA一键整理代码快捷键
Comparator接口:比较器
{方法: int compare(T o1, T o2) 返回值大于0,o1大。}
Collections.sort方法默认是按照返回结果从小到大进行排序。 如果我们需要从大到小则在实现比较器大时候将返回结果乘以-1即可。
详细使用方法: class 自定义名 implements Comparator<类名> { public int compare(类名 对象名, 类名 对象名) {
return (对象名.getGroup()-对象名.getGroup()); }
} 注意点return时候公式要用()包着
接口使用方法:public class 实现类名称 implements 接口名称
报错:No interface expected here 意思是这里没有接口。
接口格式:public interface 接口名称 实现接口public class 实现类 implements 接口名
List<Employee> list = new ArrayList<Employee>();
创建好对象后使用list.add(对象名);一个个加进去
为什么是List而不是ArrayList? 效率更高。
List是接口。 ArrayList是类。
List里的方法由ArrayList实现。 |