[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());

原文地址:https://www.cnblogs.com/Johnsonxiong/p/10147294.html

时间: 2024-08-30 12:01:55

[Java in NetBeans] Lesson 15. Sorting and Searching.的相关文章

[Java in NetBeans] Lesson 01. Java Programming Basics

这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.jar name with the same as package), one package can contains mutiple .java files. Comment mutiple lines by using "/* */"; comment one line by usin

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

这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) 1. File() : A Java representation of a file. File file = new File("test.txt"); 2. PrintWriter() : Write to a file. Write string into the file. /

[Java in NetBeans] Lesson 03. More Variables / Type Casting

这个课程的参考视频在youtube. 主要学到的知识点有: It is different from python, that "1" only present string "1", and '1' only presents char '1'. (type) can chang the type , e.g. (int) (totalScore/4.5); will change the result of(totoalScore/4.5) which is a

Java Swing界面编程(15)---JScrollPane

在一般的图形界面中,如果显示的区域不够大,往往会出现滚动条以方便用户的浏览,在Swing中JScrollPane的主要的功能就是为了显示的内容加入水平滚动条. package com.beyole.util; import java.awt.Container; import java.io.File; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.sw

Android BaseAdapter加载多个不同的Item布局时出现UncaughtException in Thread main java.lang.ArrayIndexOutOfBoundsException: length=15; index=15

java.lang.ArrayIndexOutOfBoundsException: length=15; index=15 异常出现的场景:在做聊天界面时,需要插入表情,图片,文字,名片,还有几种较为复杂的布局.这时就需要用到BaseAdapter中的getViewTypeCount()和getItemViewType(int position) 方法了,在发送复杂界面时出现了这个异常. 令人抓狂的是这个异常居然是UncaughtException,根本无法判断哪一行出错了,刚开始的时候觉得一定

lesson 15 Fifty pence worth of trouble

lesson 15 Fifty pence worth of trouble appreciate =be grateful for We really appreciate all the help you gave us last month. I appreciate your making the effort to come. 比thank you强点的谢谢 It's very nice of you. I really appreciate it. I would appreciat

Java学习lesson 15

*Set集合 一个包含重复的元素collection,并且最多包含一个null元素,此类实现Set接口,有哈希表支持,Java中的预定义类型如String.Integer都可以在集合内使用:但对于自己创建的类型是,要注意到Set 需要一种方式来维护存储顺序,而存储顺序如何维护,则是在Set的不同实现间会有所变化.因此,不同的Set实现不仅具有不同的行为,而且他们对于可以在特定是我Set抓狂那个放置的元素类型也有不同要求 继承自Collection集合,哈希表通过它的自实现类HashSet集合实例

[原创]java WEB学习笔记15:域对象的属性操作(pageContext,request,session,application) 及 请求的重定向和转发

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 ---------------------------------

《java语法实例2~15章》

第二章 1.  数据类型 变量名 =值: Eg: Double score =15.65: String name ="张三": Char sex ="男": 数据类型强转 数据类型  变量名 =(数据类型)值: 示例: Double num=15.75: Int sum =(int)num: 结果:sum=15: 第三章 1.if选择结构 语法:    if(条件){ 代码块     //条件成立后要执行的代码,可以是一条语句,也可以是一组语句 } 示例: If(