[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 ...
随机推荐
- JVM源码分析之栈溢出完全解读
概述 之所以想写这篇文章,其实是因为最近有不少系统出现了栈溢出导致进程crash的问题,并且很隐蔽,根本原因还得借助coredump才能分析出来,于是想从JVM实现的角度来全面分析下栈溢出的这类问题, ...
- Python基础爬虫
搭建环境: win10,Python3.6,pycharm,未设虚拟环境 之前写的爬虫并没有架构的思想,且不具备面向对象的特征,现在写一个基础爬虫架构,爬取百度百科,首先介绍一下基础爬虫框架的五大模块 ...
- Linux系统下升级Python版本步骤(suse系统)
Linux系统下升级Python版本步骤(suse系统) http://blog.csdn.net/lifengling1234/article/details/53536493
- 高效办公必不可少的5个Excel技巧
1.输入“001.002…”的编号 想要快速给表格添加上“001.002…”这样的编号,你可以这样做: 选择所有单元格——右键点击[设置单元格格式]——点击[文本]——点击[确定]即可. 2.单元格内 ...
- LUA 语言易混点
--代码: tab1 = { key1 = "val1", key2 = "val2","val2", "val3" , ...
- ubuntu汉化
寻找 Simplified Chinese,点击下面的apply 回到这里在最下面找到刚刚安装的语言,是灰色的,左键点击,将它拖到最上面,点击应用到整个系统,配置完成.重启系统就变成中文了. 晚上不要 ...
- Linux下稀疏文件的存储方式
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- mysql之workbench如何只导出(insert语句)数据
https://www.jianshu.com/p/a5cd14bc5499 1. 说明: 出发点: 由于特殊原因,我们只想导出数据库中的数据(insert into语句格式的),但是在网上找到的资源 ...
- iptables及其在路由器上的应用 (待完善)
1. iptables基本定义 Table (表名) Explanation (注释) nat nat表的主要用处是网络地址转换,即Network Address Translation,缩写为NAT ...
- easyui中多级表头,主表头不能添加field字段,否则不居中
<th field="" width="120" align="center" align="center" co ...