这个课程的参考视频和图片来自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.的更多相关文章

  1. [Java in NetBeans] Lesson 07. JavaDoc and Unit Tests

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. organize code into packages Create a package if want to make th ...

  2. [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 ...

  3. [Java in NetBeans] Lesson 17. File Input/Output.

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...

  4. [Java in NetBeans] Lesson 16. Exceptions.

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...

  5. [Java in NetBeans] Lesson 09. Switch / If-Else Ladder

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. Nested If-else statement (if-else ladder) /** * 90 and above == ...

  6. [Java in NetBeans] Lesson 06. Custom classes

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...

  7. [Java in NetBeans] Lesson 05. Method/function

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Define a method:(motivation: write one time, but use it many times ...

  8. [Java in NetBeans] Lesson 04. Class / Objects

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...

  9. [Java in NetBeans] Lesson 01. Java Programming Basics

    这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.j ...

随机推荐

  1. J - 哈密顿绕行世界问题

    一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行 ...

  2. LDAP - 轻量目录访问协议

    LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP

  3. STM FLASH在线编程 升级

    注意字节到 stm flash 顺序是反的 例如 12 34 56 78 世纪写入内存 应该是 78 56 34 12

  4. [No0000C5]VS2010删除空行

    VS2010删除空行,查找内容:^:b*$\n,替换为:,查找范围:当前文档,使用:正则表达式

  5. [knowledge][perl][pcre][sed] sed / PCRE 语法/正则表达式

    一直用sed一直没有正经的学过语法,一直一知半解的用着. 因为,它用来perl的语法,要想搞懂,首先要搞懂perl,系统的入个门... 之前,man sed,man了好多次,总是没找到关键内容,今天在 ...

  6. Oracle使用par文件进行全库导入导出

    expdp \"/ as sysdba\" PARFILE=/oracle/expdp/AINEWAWARD_20181005.par directory=dumpdir dump ...

  7. LeetCode 461 Hamming Distance 解题报告

    题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...

  8. springboot注入properties配置到javabean

    一.再application.properties中添加 二. @Value("${field}")在字段上面加个注解

  9. vue router拦截器的简单使用

    之前,为了实现router跳转的每个页面的url上都带上addressCode,然后用了一下router拦截器,很好用,当然也可以专门封装一个方法来实现(跳转的页面上带有addressCode),不过 ...

  10. java 网络编(二)UDP的传输

    发送端: package cn.sasa.netDemo1; import java.io.IOException; import java.net.DatagramPacket; import ja ...