} } }
************************************************************************************************************************************************************** BinarySearch
import java.util.Comparator;
public class BinarySearch {
public static
throw new IllegalArgumentException(\被查找数组为null!\); } else if (x == null) {
throw new IllegalArgumentException(\被查询元素为null!\); }
int left = 0;
int right = a.length - 1;
while (left <= right) {
int middle = (left + right) / 2; if (comparator != null) {
if (comparator.compare(x, a[middle]) == 0) return middle;
if (comparator.compare(x, a[middle]) > 0) left = middle + 1; else
right = middle - 1; }
else { if(x.compareTo(a[middle])==0) return middle;
if(x.compareTo(a[middle])>0) left = middle + 1;
else right = middle - 1; } }
return -1; }
public static void main(String[] args) { Student[] a = new Student[4];
36
a[0] = (new Student(3, \)); a[1] = (new Student(5, \)); a[2] = (new Student(7, \)); a[3] = (new Student(9, \));
Student x = new Student(7, \);// 设定要查找的数
int position = binarySearch(a, x, new StudentComparator()); System.out.println(x + \在数组中的位置是\ + position);
} }
Student
import java.util.TreeSet; import java.util.Iterator;
public class Student implements Comparable
private String name;
public Student (int id, String name) { this.id = id;
this.name = name; }
public void setId (int id) { this.id = id; }
public void setName (String name) { this.name = name; }
public int getId () { return id; }
public String getName () { return name; }
37
/* Student 类的字符串表达式,形如: * 2 张三 */
public String toString () {
return (id + \ }
/* 实现 Comparable 接口中的 compareTo 方法, * 通常大于时返回一个正数,小于时返回一个负数, * 等于时返回零,具体情况可以自行决定。
********************************************************* * 这里我根据 id 号的大小进行了比较。由于 TreeSet * 会根据 compareTo 的结果来排序,因此输出结果 * 应该是按照 id 号从小到大排序的。
* 如果要根据姓名进行排序,只需对这个方法进行相应的修改。*/
public int compareTo (Student arg) { if (id > arg.id) return 1; else if (id == arg.id) return 0; else
return -1; }
/* 以下为主方法,输出结果是: * 3 John * 5 Tom * 7 Alice * 9 David
* 可以看到不同于输入顺序, * TreeSet 已经将其排序了。*/
public static void main (String args[]) {
TreeSet
Iterator
38
System.out.println(itor.next().toString()); } } }
StudentComparator
import java.util.Comparator; /**
* Student 比较器 */
public class StudentComparator implements Comparator
@Override
public int compare(Student s1, Student s2) {
if(s1.getId()>s2.getId()) return 1;
else if(s1.getId()==s2.getId()) return 0; else
return -1; } }
39
package test1; /**
* Title: Hello Java World
* Description: 简单的Java程序,只显示一个信息。 * filename: HelloWorld.java */
public class HelloWorld {
public static void main(String[] args) { System.out.println(\ } }
package test2; /**
* Title: Java语言流程演示
* Description: 演示Java中几种常用的流程控制操作 * Filename: flowDome.java */
public class flowDemo{
public static void main(String[] arges){ int iPara1,iPara2,iEnd; if(arges.length!=3) {
System.out.println(\ System.out.println(\比较条件1,数字类型\ System.out.println(\比较条件2,数字类型\ System.out.println(\:循环次数\
System.out.println(\ return; }else{
iPara1 = Integer.parseInt(arges[0]); iPara2 = Integer.parseInt(arges[1]); iEnd = Integer.parseInt(arges[2]); }
//if语句
if(iPara2>iPara1) {
System.out.println(\条件满足!\
System.out.println(\第2个数比第1个数大!\
40