Java基础之一组有用的类——使用二叉树搜索算法搜索某个作者(TryBinarySearch)
控制台程序。
Arrays类中的binarySearch()静态方法使用二叉树搜索算法,在有序数组中查找包含给定值的元素。只有当数组的元素按升序方式排序时,该方法才是最有效的,否则就应在调用binarySearch()方法之前调用sort()方法。
binarySearch()方法的所有版本都返回int类型的值,也就是在array中找到value的索引位置。当然,value也可能不在数组中,此时返回一个负整数。计算过程是:提取大于value的第一个元素的索引位置,翻转元素的取值符号后再减1.
public class Person implements Comparable<Person> {
// Constructor
public Person(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
}
@Override
public String toString() {
return firstName + " " + surname;
}
// Compare Person objects
public int compareTo(Person person) {
int result = surname.compareTo(person.surname);
return result == 0 ? firstName.compareTo(person.firstName) : result;
}
public String getFirstName() {
return firstName;
}
public String getSurname() {
return surname;
}
private String firstName; // First name of person
private String surname; // Second name of person
}
import java.util.Arrays;
public class TryBinarySearch {
public static void main(String[] args) {
Person[] authors = {
new Person("Danielle", "Steel"), new Person("John", "Grisham"),
new Person("Tom", "Clancy"), new Person("Christina", "Schwartz"),
new Person("Patricia", "Cornwell"), new Person("Bill", "Bryson")
};
Arrays.sort(authors); // Sort using Comparable method
System.out.println("\nOrder after sorting into ascending sequence:");
for(Person author : authors) {
System.out.println(author);
}
// Search for authors
Person[] people = {
new Person("Christina", "Schwartz"), new Person("Ned", "Kelly"),
new Person("Tom", "Clancy"), new Person("Charles", "Dickens")
};
int index = 0;
System.out.println("\nIn search of authors:");
for(Person person : people) {
index = Arrays.binarySearch(authors, person);
if(index >= 0) {
System.out.println(person + " was found at index position " + index);
} else {
System.out.println(person + " was not found. Return value is " + index);
}
}
}
}
Java基础之一组有用的类——使用二叉树搜索算法搜索某个作者(TryBinarySearch)的更多相关文章
- Java基础之一组有用的类——为标记定义自己的模式(ScanString)
控制台程序. Scanner类提供了一种方式,用来指定如何识别标记.这需要使用next()方法的两个重载版本.其中的一个版本接受Pattern类型的参数.另一个版本接受String类型的参数,用来指定 ...
- Java基础之一组有用的类——使用Scanner对象(TryScanner)
控制台程序. java.util.Scanner类定义的对象使用正则表达式来扫描来自各种源的字符输入,并把输入显示为各种基本类型的一系列标记或者显示为字符串. 默认情况下,Scanner对象读取标记时 ...
- Java基础之一组有用的类——使用正则表达式搜索子字符串(TryRegex)
控制台程序. 正则表达式只是一个字符串,描述了在其他字符串中搜索匹配的模式.但这不是被动地进行字符序列匹配,正则表达式其实是一个微型程序,用于一种特殊的计算机——状态机.状态机并不是真正的机器,而是软 ...
- Java基础之一组有用的类——使用公历日历(TryCalendar)
控制台程序. 公历是西方使用的日历,用GregorianCalendar类的对象来表示.GregorianCalendar对象封装了时区信息.日期和时间数据.GregorianCalendar对象有7 ...
- Java基础之一组有用的类——生成日期和时间(TryDateFormats)
控制台程序. java.util包中含有相当多的类涉及日期和时间,包括Date类.Calendar类和GregorianCalendar类. Date类对象其实定义了精确到毫秒的时刻,从1970年1月 ...
- Java基础之一组有用的类——Observable和Observer对象(Horrific)
控制台程序. Obserable类提供了一个有趣的机制,可以把类对象中发生的改变通知给许多其他类对象. 对于可以观察的对象来说,类定义中需要使用java.util.Observable类.只需要简单地 ...
- Java基础之一组有用的类——使用比较器对数组排序(TrySortingWithComparator)
控制台程序. Arrays类中的sort()静态方法把传送为参数的数组元素按升序方式排序. 对于第一个参数类型是Object[]的sort()方法来说,可以传送任意类型的数组.如果使用sort()方法 ...
- Java基础之一组有用的类——使用正则表达式查找和替换(SearchAndReplace)
控制台程序. 使用正则表达式执行查找和替换操作,只需要调用Matcher对象的find()方法,就可以调用appendReplacement()方法来替换匹配的子序列.在提供给方法的新StringBu ...
- Java基础-类加载机制与自定义类Java类加载器(ClassLoader)
Java基础-类加载机制与自定义类Java类加载器(ClassLoader) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于类加载器的概念和分类我就不再废话了,因为我在之前的笔 ...
随机推荐
- Yii源码阅读笔记(二)
接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...
- PHP5.4安装xhprof扩展[不要去pecl下载]
HP5.3或之前版本可以去pecl(http://pecl.php.net)下载xhprof扩展安装. 但pecl上的版本不支持PHP5.4 可以到github上的xhprof库中下载:https:/ ...
- B-Tree indexs
mysql_High.Performance.MySQL.3rd.Edition.Mar.2012 A B-Tree index speeds up data access because the s ...
- filesort
- jsmooth 中文乱码
为了一个问题 语言国际国际化 测试了这么多回 ,真佩服自己 jsmooth 中文乱码 语言乱码 的解决办法 : 需要在“JVM” 的参数 中填入一项 : user.language=en 而不是 ...
- Lambda 中如果构建一个查询条件,扔该Where返回我们需要的数据。
有一个需求,比如所 省市县 这三个查询条件 都可能有可能没有,但是我们的查询条件怎么构建呢 首先需要看一下 Lambda中Where这个方法需要什么参数 public static IEnumerab ...
- Linq分组功能
Linq在集合操作上很方便,很多语法都借鉴自sql,但linq的分组却与sql有一定的区别,故整理发布如下. 1. Linq分组 分组后以Key属性访问分组键值. 每一组为一个IEnumberAbl ...
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click ...
- Housse Robber II | leetcode
可以复用house robber的代码,两趟dp作为两种情况考虑,选最大值 #include <stdio.h> #define MAX 1000 #define max(a,b) ( ( ...
- java字符串和unicode互转
直接上代码 private static String decodeUnicode(String input) { if (null == input) return input; int len = ...