目录
一、Math类的应用
1、Math类的概述
Math包含执行基本数字运算的方法
2、Math中方法的调用方式
Math类中无法构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
3、Math类的常用方法
方法名 | 说明 |
public static int abs(int a) | 返回参数的绝对值 |
public static double ceil(double a) | 返回大于或等于参数的最小double值,等于一个整数 |
public static double floor(double a) | 返回小于或等于参数的最大double值,等于一个整数 |
public static int round(float a) | 按照四舍五入返回最接近参数的int |
public static int max(int a,int b) | 返回两个int值中的较大值 |
public static int min(int a,int b) | 返回两个int值中的较小值 |
public static double pow(doublie a,double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的正值,[0.0,1.0) |
二、System类的应用
System类的常用方法
方法名 | 说明 |
public static void exit(int status) | 终止当前运行的Java虚拟机,非零表示异常终止 |
public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
示例代码:
需求:在控制台输出1-10000,计算这段代码执行了多少毫秒
public class SystemDemo {
public static void main(String[] args) {
// 获取开始的时间节点
long start = System.currentTimeMillis();
for (int i = 1; i <= 10000; i++) {
System.out.println(i);
}
// 获取代码运行结束后的时间节点
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end start) + "毫秒");
}
}
三、Object类的toString方法
Object类概述:
Object是类层次结构的根,每个类都可以将Object作为超类。所有类都直接或者间接继承自该类,换句话说,该类所具备的方法,所有类都会有一份。
IDEA查看源码的方式:
选中方法,按下Ctrl+B
重写toString方法的方式:
1、Alt+Insert选择toString
2、在类的空白区域,右键 -> Generate ->选择toString
toString方法的作用:
以良好的格式,更方便的展示对象中的属性值
示例代码:
class Student extends Object {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("小明");
s.setAge(18);
System.out.println(s);
System.out.println(s.toString());
}
}
四、Object类的equals方法
equals方法的作用:
用于对象之间的比较,返回true和false的结果
举例:s1.equals(s2); s1和s2是两个对象
重写equals方法的场景:
不希望比较对象的地址值,想要结合对象属性进行比较的时候
重写equals方法的方式:
1、alt+insert选择equals() and hashCode(),Intellij Default,一路next,finish即可
2、在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上
示例代码:
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
//this s1
//o s2
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o; //student s2
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("c小明");
s1.setAge(18);
Student s2 = new Student();
s2.setName("小明");
s2.setAge(18);
//需求:比较两个对象的内容是否相同
System.out.println(s1.equals(s2));
}
}
五、冒泡排序原理
● 冒泡排序概述:
一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在 后面,依次对所有的数据进行操作,直至所有数据按要求完成排序
● 如果有n个数据进行排序,总共需要比较n-1次
● 每一个比较完毕,下一次的比较就会少一个数据参与
代码实现:
/*
冒泡排序:
一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,
依次对所有的数据进行操作,直至所有数据按要求完成排序
*/
public class ArrayDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {24, 69, 80, 57, 13};
System.out.println("排序前:" + arrayToString(arr));
// 这里减1,是控制每轮比较的次数
for (int x = 0; x < arr.length 1; x++) {
// 1是为了避免索引越界,x是为了调高比较效率
for (int i = 0; i < arr.length 1 x; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
System.out.println("排序后:" + arrayToString(arr));
}
//把数组中的元素按照指定的规则组成一个字符串:[元素1, 元素2, ...]
public static String arrayToString(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(", ");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
}