跟着黑马的Java学习视频学者本篇常用的API
Math
public static int abs(int a) {return (a < 0) ? -a : a;
}public static double ceil(double a) {return StrictMath.ceil(a);
}public static double floor(double a) {return StrictMath.floor(a);
}public static double pow(double a, double b) {return StrictMath.pow(a, b); // default impl. delegates to StrictMath}
}public static int max ( int a, int b){return (a >= b) ? a : b;
}
System
(1)虚拟机停止 System.exit(0);
System.out.println("hello1");
System.exit(0);
System.out.println("hello2");
(2) 返回当前系统的时间毫秒值形式 public static long currentTimeMillis()
public class TestCurrentTimeMillis {public static void main(String[] args) {long start1 = System.currentTimeMillis();for (int i = 2; i < 10000; i++) {if (isPrimer1(i)) {System.out.println(i);}}long end1 = System.currentTimeMillis();System.out.println("isPrimer1 execution time: " + (end1 - start1) + "ms");long start2 = System.currentTimeMillis();for (int i = 2; i < 10000; i++) {if (isPrimer2(i)) {System.out.println(i);}}long end2 = System.currentTimeMillis();System.out.println("isPrimer2 execution time: " + (end2 - start2) + "ms");}// 判断一个数是否为质数(未优化)public static boolean isPrimer1(int number) {if (number <= 1) return false;for (int i = 2; i < number; i++) {if (number % i == 0) {return false;}}return true;}// 判断一个数是否为质数(优化,只需检查到其平方根)public static boolean isPrimer2(int number) {if (number <= 1) return false;for (int i = 2; i <= Math.sqrt(number); i++) {if (number % i == 0) {return false;}}return true;}}
(3)System.arraycopy方法进行数组拷贝
对于基本数据类型数组(如int[]):
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int[] arr2 = new int[10];System.arraycopy(arr1, 0, arr2, 4, 3);
对于引用数据类型数组(这里假设有一个父类Person和子类Student的关系):
Student s1 = new Student("张三", 23);Student s2 = new Student("李四", 24);Student s3 = new Student("王五", 25);Student[] brr1 = {s1, s2, s3};Person[] brr2 = new Person[3];System.arraycopy(brr1, 0, brr2, 0, 3);
for (int i = 0; i < brr2.length; i++) {// 强转如果数据源数组和目的地数组都是引用数据类型,那么子类类型可以赋值给父类类型Student stu=(Student) brr2[i];System.out.println(stu.getName() + " " + stu.getAge());}
object是顶级父类,只有无参构造
/*object是顶级父类,只有无参构造*/
public class APi_03Object {public static void main(String[] args) {// Student类的实例化和equals()方法使用部分已注释掉String str = "abc";StringBuilder sb = new StringBuilder("abc");// 比较String对象和StringBuilder对象的内容是否相等System.out.println(str.equals(sb.toString())); // true,因为sb.toString()返回"abc"System.out.println(sb.toString().equals(str)); // true,同样比较的是字符串内容// 以下两行代码会抛出编译错误,因为StringBuilder的equals()方法不会直接与String比较// System.out.println(str.equals(sb));// System.out.println(sb.equals(str));}
}
Clone
把A对象的属性值完全拷贝给B对象,
也叫对象拷贝,对象复制
基本数据类型: 存储的是真实的值
引用数据类型: 存储的是另一个空间的地址值
(1)浅克隆
不管对象内部的属性是基本数据类型还是引用数据类型,都完全拷贝过来
public static void main(String[] args) {int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};User u1 = new User(1, "张三", "123", "CC", data);User u2 = (User) u1.clone();System.out.println(u1.arrToString());System.out.println(u2.arrToString());// 验证浅克隆int[] arr = u1.getData();arr[0] = 100;System.out.println(u1.arrToString());System.out.println(u2.arrToString());
}
(2)深克隆
基本数据类型拷贝过来字符串复用引用数据类型会重新创建新的
@Override
protected Object clone() throws CloneNotSupportedException {// 先把克隆对象中的数组获取出来int[] data = this.data;
// 创建新的数组int[] newdata = new int[data.length];for (int i = 0; i < data.length; i++) {newdata[i] = data[i];}User u = (User) super.clone();u.data = newdata;return u;
}
public static void main(String[] args) {int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};User u1 = new User(1, "张三", "123", "CC", data);User u2 = (User) u1.clone();System.out.println(u1.arrToString());System.out.println(u2.arrToString());// 验证浅克隆int[] arr = u1.getData();arr[0] = 100;System.out.println(u1.arrToString());System.out.println(u2.arrToString());
}