这个课程的参考视频和图片来自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. es7 class装饰器

    文档http://es6.ruanyifeng.com/#docs/decorator ts文档 https://www.tslang.cn/docs/handbook/decorators.html ...

  2. nodejs--get请求数据解析

    ---- 三种方式解析: 1.自动动手切 2.api的querystring模块 3.api的url模块

  3. DB2 rollforward 命令使用详解

    DB2 rollforward 命令使用详解 原文:https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1003wuc ...

  4. C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)

    一.通过WebClient获取网页内容 这是一种很简单的获取方式,当然,其它的获取方法也很简单.在这里首先要说明的是,如果为了实际项目的效率考虑,需要考虑在函数中分配一个内存区域.大概写法如下 //M ...

  5. [No0000151]菜鸟理解.NET Framework中的CLI,CLS,CTS,CLR,FCL,BCL

    最下层蓝色部分是.NET Framework的基础,也是所有应用软件的基础..NET Framework不是凭空出来的,实际上API,COM+,和一些相关驱动依然是它的基石..NET Framewor ...

  6. MVC模型和MVT模型

    MVC 大部分开发平台都需要搭建的后台框架,Java和PHP最为喜爱 M:model.     模型.      主要用于对数据库层的封装 V:view.        视图.      用于对用户展 ...

  7. es6/ts for in/ for of

    for in 是es6之前就有的循环下标的方式 for of 是typescript的循环对象或者数组中值的方式,但是不能循环普通的对象,需要通过和Object.keys()搭配使用,如果循环普通对象 ...

  8. 使用flask写移动端API

    环境 python 3.7 使用pip 安装falsk pip3 install flask #!flask/bin/python from flask import Flask, jsonify, ...

  9. day0321正则表达式

    一.正则表达式 1.定义一个规则,检测某一段字符串是否符合规则,将符合规则的字符匹配出来. 2.只和字符串相关 3.字符组 描述一个字符位置的内容 3.1    [012345]检测0,1,2,3,4 ...

  10. iOS自定义结构体

    一.提要 通过以官方的CGSize为例,自定义Objective-C中的结构体,并使用. 二.CGSize 1.系统定义的CGSize结构体 struct CGSize { CGFloat width ...