二分查找
public class ArrayUtil {
/*
分析:二分法查找,将有序数组的长度除以2,(数组第一个下标+最后一个下标)/2得到的是数组的中间值,如果要查找的小于中间值就在左边查,如果大于中间值,就在右边查
右边查将中间值下标+1赋给记录数组第一个下标的变量,如果是左边查,将中间变量-1赋给记录数组最后一个变量,表示从左边查,这样循环,直到查到。
int a[] = {2,4,6,8,22,44},查44
定义变量head为第一个下标即为0,定义end为最后一个下标即为5;
求中间变量mid = (0+5)/2,判断44<a[2]?从右边查 head = mid+1
mid = (head + end)/2=4,判断44<a[4]?从右边查 head = mid + 1
mid = (head + end)/2=4,判断44<a[4]?从右边查 head = mid + 1
mid = (head + end)/2=4,判断44=a[4]?等于,查到
*/
public static int BinarySearch(int[] a,int x) {
int head = 0; //头
int end = a.length-1; //尾
int mid = 0; //中间值
/*int y = 0;
for (int i = 0; i < a.length/2; i++) {
mid = (head + end)/2;
if (x < a[mid]) {
end = mid - 1;
} else head = mid + 1;
if (x == a[mid]) y = mid;
}
return y;*/
//方法2
while (head<=end) {
mid = (head + end)>>1; //>>相当于/2
if (x < a[mid]) {
end = mid - 1;
} else head = mid + 1;
if (x == a[mid]) return mid;
}
return -1;
}
public static void main(String[] args) {
int a[] = {2,4,6,8,22,44,55};
int i = BinarySearch(a, 0);
if (i==-1) System.out.println("不存在!");
else System.out.println(i);
}
}
常用类
String
- jvm不会回收方法区字符串常量池里面的内容
- String类型之间比较实用equals,低层重写equals方法
- String类已经重写了toString方法
2、StringBuilder和StringBuffer
- StringBuilder,线程不安全
- StringBuffer,线程安全,里面的方法都有synchronize关键字修饰
BigDecimal(处理财务数据,精确度极高)
- 求和:使用add()方法
- 除:divide()
任务
- 编写 程序,生成5个不重复的随机数,重复的话重新生成,最终生成的5个随机数放到数组中,要求数组中的5个随机数不重复
/**
* 编写 程序,生成5个不重复的随机数,重复的话重新生成,最终生成的5个随机数放到数组中,要求数组中的5个随机数不重复
*/
public class MyRandom {
public static void main(String[] args) {
int a[] = new int[5];
Random random = new Random();
for (int i = 0; i < a.length; i++) {
a[i] = -1;//给一个默认值
}
int index = 0;
while (index<a.length) {
int number = random.nextInt(11);
//判断数组里面有不有生成的这个数,如果有,就不在赋值,没有就赋值
if (!contains(a,number)) {
a[index++] = number;
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public static boolean contains(int a[], int key) {
for (int i = 0; i < a.length; i++) {
if (a[i] == key) return true;
}
return false;
}
}
- 模拟蓝墨云摇一摇,从所有人当中抽三个人回答问题,不能重复
package com.feng.random;
import java.util.Objects;
public class Student {
private String id;
private String name;
private String cls;
public Student() {
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", cls='" + cls + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(id, student.id) &&
Objects.equals(name, student.name) &&
Objects.equals(cls, student.cls);
}
@Override
public int hashCode() {
return Objects.hash(id, name, cls);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
public Student(String id, String name, String cls) {
this.id = id;
this.name = name;
this.cls = cls;
}
}
package com.feng.random;
import java.util.Random;
/**
* 模拟蓝墨云摇一摇,从所有人当中抽三个人回答问题,不能重复
*/
public class RandomShake {
public static void main(String[] args) {
Student s = new Student("20174221055","峰","计科1702");
Student s1 = new Student("20174221054","排面","计科1702");
Student s2 = new Student("20174221053","刘","计科1702");
Student s3 = new Student("20174221052","李","计科1701");
Student s4 = new Student("20174221051","张","计科1702");
Student s5 = new Student("20174221050","杨","计科1701");
Student s6 = new Student("20174221032","姜","计科1702");
Student students[] = {s,s1,s2,s3,s4,s5,s6,};
Student b[] = random(students,3);
for (Student student : b) {
System.out.println(student);
}
}
/**
* 随机在A数组里面找X个数
* @param a 在a数组里面抽x个数
* @param x 随机抽取个数
* @return 随机的抽的x个元素
*/
public static Student[] random(Student[] a,int x) {
int n = a.length;
//定义一个数组用于存储返回的结果,也就是随机返回的三个数
Student b[] = new Student[x];
for (int i = 0; i < b.length; i++) {
Random random = new Random();
int r = random.nextInt(n);
//将随机数作为下标,将该下标所对应的元素作为返回数组的第一个元素
b[i] = a[r];
//避免重复,将数组最后一个元素与抽到的元素的位置交换,并且数组长度-1,就避免了下次抽到最后一个元素位置的下标,所以随机生成的数的范围也要-1
a[r] = a[n-1];
n--;
}
return b;
}
}
异常
1、什么是异常,java异常机制有什么用?
- 程序在执行过程中发现了不正常的情况,就称之为异常
- 在 程序执行过程中出现了不正常的情况,Java把改异常捕获到,并在控制台打印输出,供程序员参考,修复该程序,让程序更加的健壮。