[Java in NetBeans] Lesson 15. Sorting and Searching.
这个课程的参考视频和图片来自youtube。
主要学到的知识点有:
Build in functions in java.util.Collections
- Need to implement a comparator - a special class which returns an integer comparision of two object, if compare(a,b), if return negative number, a will be before b, otherwise a will be after b. (Just need to override the compare() function)
1. Sorting: arrange a collection in order Collections.sort(List<T> list, Comparator<>)
It is used like below:
ArrayList<integer> numbers = new ArrayList<>();
for (int i =0; i< 20; i++){
numbers.add(generator.nextInt(100) + 1); // get a random number from 1 to 100
}
Collections.sort(numbers, new IntegerComparator())
Then we create a comparator, defined in another java class called IntegerComparator
public class IntegerComparator implements Comparator<integer>{
@Override
public int compare(Integer a, Integer b){
return a-b;
}
}
If sometimes we need to compare two objects of a customed class.
- Here assume that we have a class called Student, it contains GPA and name of the student. Then we will implement the class of StudentGpaComparator.
import java.util.Comparator; public class StudentGpaComparator implements Comparator<Student>{ @Override
public int compare(Student s1, Student s2){
double gpa1 = s1.getGpa();
double gpa2 = s2.getGpa();
return (int) ((gpa1 - gpa2)*100)
}
}
2. Search: find a specific value in a collections Collections.binarySearch(List<T> list, T key, Comparator<>)
It is used like below: (will return -1 if not found)
Collections.binarySearch(numbers, 50, new IntegerComparator());
[Java in NetBeans] Lesson 15. Sorting and Searching.的更多相关文章
- [Java in NetBeans] Lesson 07. JavaDoc and Unit Tests
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. organize code into packages Create a package if want to make th ...
- [Java in NetBeans] Lesson 00. Getting Set-up for Learning Java
这个课程的参考视频在youtube. 主要学到的知识点有: set up needs Java SE JDK, NetBeans IDE class name should be the same l ...
- [Java in NetBeans] Lesson 17. File Input/Output.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...
- [Java in NetBeans] Lesson 16. Exceptions.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...
- [Java in NetBeans] Lesson 09. Switch / If-Else Ladder
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. Nested If-else statement (if-else ladder) /** * 90 and above == ...
- [Java in NetBeans] Lesson 06. Custom classes
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...
- [Java in NetBeans] Lesson 05. Method/function
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Define a method:(motivation: write one time, but use it many times ...
- [Java in NetBeans] Lesson 04. Class / Objects
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...
- [Java in NetBeans] Lesson 01. Java Programming Basics
这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.j ...
随机推荐
- apache 二级域名设置完整步骤
步骤如下: 1. 你要拥有一个有泛域名解析的顶级域名,例如:abc.com 在dns服务上设置,域名服务商都提供此服务 www.abc.com 指向服务器IPabc.com ...
- Thrift IDL
Thrift类型 Thrift类型系统旨在允许程序员尽可能使用本机类型,无论使用何种编程语言.此信息基于并取代Thrift白皮书中的信息.Thrift IDL为每一种目标语言提供了用于生成代码的类型描 ...
- 证书文件(pfx)读取时报 “指定的网络密码不正确”
实际情况: 1.本地测试正确,发布到windows server 2003 iis6 可以正常运行 发布到 windows server 2008 上 II7就报 “指定的网络密码不正确” 日志报错为 ...
- CSS中隐藏内容的3种方法
CSS中隐藏内容的3种方法 一般有:隐藏文本/图片.隐藏链接.隐藏超出范围的内容.隐藏弹出层.隐藏滚动条.清除错位和浮动等. 1.使用display:none来隐藏所有内容 display:none可 ...
- idea 乱码问题
1. db browser查询结果为乱码: 找到idea的安装目录 如C:\..\Roaming\JetBrains\IntelliJ IDEA Community Edition 2018.1.3\ ...
- Mac开发博客摘录
https://blog.csdn.net/wangyouxiang/article/details/17855255 https://www.cocoacontrols.com/controls?p ...
- centos6.5设置key登录
1.ssh-keygen -t rsa 一路回车,当然可以设置key密码 2.cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_key ...
- [daily][dpdk] 内核模块(网卡驱动)无法卸载
由于程序的异常退出, 内核的引用计数没有被清除(我猜的). 所以驱动不能被卸载掉, 强制也不行. 如下: [root@localhost ~]# insmod /opt/scorpion/KingKo ...
- 读书笔记-iOS核心动画高级技巧
如果不使用+imageNamed:,那么把整张图片绘制到CGContext可能是最佳的方式了. 这里我们利用了CALayer的KVC来存储和检索任意的值,将图层和索引打标签. 使用KVC打标签
- python发送邮件 大全汇总
https://blog.csdn.net/bmxwm/article/details/79007871 参考菜鸟教程发送只有文字的邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...