[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 ...
随机推荐
- js document.activeElement 获得焦点的元素
<body> <input type="text" id="test" value="ajanuw"> <sc ...
- windows 上的 neovim 配置
可以使用简单的 linux 下 neovim 配置,增加了对 golang, python, ruby 脚本文件一键运行快捷方式. """""&qu ...
- Jenkins插件管理
1.配置jenkins需要的maven.jdk路径 [root@db01 secrets]# echo $JAVA_HOME /application/jdk [root@db01 secrets]# ...
- HDU 5985/nowcoder 207D - Lucky Coins - [概率题]
题目链接:https://www.nowcoder.com/acm/contest/207/D 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5985 ...
- PostgreSQL源码安装文档
This document describes the installation of PostgreSQL using the source code distribution. (If yo ...
- spark运行wordcount程序
首先提一下spark rdd的五大核心特性: 1.rdd由一系列的分片组成,比如说128m一片,类似于hadoop中的split2.每一个分区都有一个函数去迭代/运行/计算3.一系列的依赖,比如:rd ...
- monit安装配置
环境centos5(32bit),monit-5.17.1,下载地址 https://bitbucket.org/tildeslash/monit/downloads/ 1.tar zxvf moni ...
- rsync定时同步文件
rsync服务器 ip:192.168.1.198 操作系统:centos7.2 rsync客户端 ip:192.168.1.16 操作系统:centos7.2 服务器配置 1.yum -y inst ...
- 单调性 [1 + 1 / (n)]^n
def f(n): n += 0.0 s = 1 + 1 / (n) r = pow(s, n) print(n, ',', r) return r l = []for i in range(1, 1 ...
- go install and go captcha
https://blog.csdn.net/liuhongwei123888/article/details/8512815 [gocaptcha] http://www.cnblogs.co ...