文章目录
5 第五章JavaAPI
是对java预先定义的类或接口, 功能 和方法功能的说明文档,目的是提供给开发人员进行使用帮助说明。
5.1 基本数据类型包装类
Java是一门面向对象的语言,但是其八种基本数据类型却是不面向对象的,这在实际使用中造成了很多不方便,为此引入了包装类的概念。
用途:
- 方便涉及到对象时的操作
- 包含基本数据类型的相关属性和操作
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
character | Character |
boolean | Boolean |
5.2 Object
java.lang.Object
方法 | 作用 |
---|---|
String toString() | 返回描述该对象值的字符串。在自定义类中应覆盖这个方法 |
boolean equals(Object otherObject) | 比较两个对象是否相等。在自定义类中应覆盖这个方法 |
Class getClass() int hashCode() | 返回包含对象信息的类对象 返回对象的散列码 |
static wait() static notify() static notifyAll() |
转成字符串toString()
返回描述该对象值的字符串
比较equals()
比较对象的地址是否相同 相当于 “==”
返回哈希值hashCode()
返回地址的hash值
5.3 Arrays
方法 | 作用 |
---|---|
static String toString(type[] a) | 返回包含a中数据元素的字符串 |
static void sort(type[] a) | 采用优化的快速排序算法对数组进行排序 |
static void binarySearch(type[] a, type v) | 使用二分搜索算法查找值v |
static Boolean equals(type[] a, type[] b) | 如果两个数字相同,返回true |
5.3.1 转成字符串toString()
public static void main(String[] args) {
int[] a = {
1,2,3};
int[] b = {
1,2,3};
System.out.println(Arrays.equals(a, b)); //比较值
System.out.println(a == b);//比较对象(也就是地址)
}
5.3.2 比较equals()
public static void main(String[] args) {
int[] a = {
1,2,3};
int[] b = {
1,2,3};
System.out.println(Arrays.equals(a, b)); //比较值
System.out.println(a == b);//比较对象(也就是地址)
}
5.3.3 排序 sort()(快速排序)
对普通类型的排序
public class SortDemo {
public static void main(String[] args) {
int[] a = {
4,7,5,3,9,2,1,8};
// Arrays.sort(a);//升序排序
Arrays.sort(a,0,4);// 区间排序
Integer[] b = {
4,7,5,3,9,2,1,8};
Arrays.sort(b); //也可以对Integer包装类数组进行排序
System.out.println(Arrays.toString(b));
}
}
对象数组的排序
public class SortDemo2 {
public static void main(String[] args) {
Student s1 = new Student("jim1",101);
Student s2 = new Student("jim2",102);
Student s3 = new Student("jim3",103);
Student s4 = new Student("jim4",104);
Student[] stuArr = {
s1,s4,s3,s2};
Arrays.sort(stuArr); // 对对象数组进行排序
/*
Student类实现了Comparator接口
sort排序调用了Student类中的compare()方法
要想用sort对对象数组进行排序,必须先实现ComParactor接口中的compare()方法。
*/
System.out.println(Arrays.toString(stuArr));
}
}
public class Student implements Comparator<Student> {
public String name;
public int id;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public Student() {
}
/**
* 基本数据类型的比较 用Comparator 接口
* @param o1
* @param o2
* @return
*/
@Override
public int compare(Student o1, Student o2) {
// return o1.id - o2.id; // o1 - o2 正序
return o2.id - o1.id; // 逆序
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
自定义排序
自定义排序 必须实现Comparator接口
用name对Student数组进行排序
对字符串的比较排序会使用到String类实现Comparable接口中的compareTo()方法
/**
*自定义排序 必须实现Comparator接口
*/
public class SortName implements Comparator<Student> {
public SortName() {
}
@Override
public int compare(Student o1, Student o2) {
// sort排序时会调用该函数
return o2.name.compareTo(o1.name);
}
}
public class SortDemo2 {
public static void main(String[] args) {
Student s1 = new Student("jim1",101);
Student s2 = new Student("jim2",102);
Student s3 = new Student("jim3",103);
Student s4 = new Student("jim4",104);
Student[] stuArr = {
s1,s4,s3,s2};
//要把你写好的自定义排序类的对象传进去,这样他会自动调用你的自定义排序compare()方法
Arrays.sort(stuArr,new SortName());
System.out.println(Arrays.toString(stuArr));
}
}
用id对Student数组进行排序
public class SortId implements Comparator<Student> {
public SortId() {
}
@Override
public int compare(Student o1, Student o2) {
return o1.id - o2.id; // 正序
}
}
public class SortDemo2 {
public static void main(String[] args) {
Student s1 = new Student("jim1",101);
Student s2 = new Student("jim2",102);
Student s3 = new Student("jim3",103);
Student s4 = new Student("jim4",