[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 ...
随机推荐
- electron 主进程,和渲染进程的通信
ipcMain https://electronjs.org/docs/api/ipc-main 当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息, 当然也有可能从主进程向渲染进 ...
- 离线安装 Android 4.0 SDK
现在 4.0 的SDK已经可以下载, 开发者应该立刻去下载安装最新的SDK,然后打开4.0的模拟器 在上面测试自己的程序是否能够正常运行. 对于下载SDK比较慢的用户, 可以直接使用其他下载工具(比如 ...
- VBS数组导出到Excel
<script language="vbscript"> dim arr(9999,4) for i=0 to 9999 for j = 0 to 4 arr(i,j) ...
- 泡泡一分钟:Stabilize an Unsupervised Feature Learning for LiDAR-based Place Recognition
Stabilize an Unsupervised Feature Learning for LiDAR-based Place Recognition Peng Yin, Lingyun Xu, Z ...
- python中的日志,logger用法
python中自带logger模块,实现方法有两种,一般使用第二种,更灵活 方法一: import logging # 通过logging.basicConfig完成 logging.basicCon ...
- Codeforces 977F - Consecutive Subsequence - [map优化DP]
题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...
- [No000017E]改善C#程序的建议7:正确停止线程
开发者总尝试对自己的代码有更多的控制.“让那个还在工作的线程马上停止下来”就是诸多要求中的一种.然而事与愿违,这里面至少存在两个问题: 第一个问题是:正如线程不能立即启动一样,线程也并不能说停就停.无 ...
- [No0000B2]ReSharper操作指南3/16-配置ReSharper与代码校错
配置ReSharper ReSharper功能具有默认配置,这些配置基于.NET世界中的约定和最佳实践.但是,每个功能都可以根据您的需求和喜好灵活调整. ReSharper首选项可以在以下位置进行配置 ...
- tomcat运行模式
Tomcat Connector的三种不同的运行模式性能相差很大 这三种模式的不同之处如下: BIO: 一个线程处理一个请求.缺点:并发量高时,线程数较多,浪费资源. Tomcat7或以下,在Linu ...
- 优化网站设计(七):避免在CSS中使用表达式
前言 网站设计的优化是一个很大的话题,有一些通用的原则,也有针对不同开发平台的一些建议.这方面的研究一直没有停止过,我在不同的场合也分享过这样的话题. 作为通用的原则,雅虎的工程师团队曾经给出过35个 ...