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(double a, double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的正值, [0.0, 1.0) |
System 类的常用方法
方法名 | 说明 |
---|---|
public static void exit(int status) | 终止当前运行的Java虚拟机, 非零表示异常终止 |
public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
Object 类的常用方法
方法名 | 说明 |
---|---|
public String toString() | 返回对象的字符串表示形式. 建议所有子类重写该方法, 自动生成 |
public boolean equals(Object obj) | 比较对象是否相等. 默认比较地址, 重写可以比较内容, 自动生成 |
IDEA 快捷键
Ctrl + B: 进入方法内部, 查看源代码
Alt + Insert: 选择toString(), 即可自动重写toString()方法
Alt + Insert: 选择equals() and hashCode(), 选择Template: Intellij Default. 继续点击下一步生成方法
冒泡排序
一种排序的方式, 对要进行排序的数据中相邻的数据进行两两比较, 将较大的数据放到后面, 依次对所有的数据进行操作, 直至所有的数据按照要求完成排序.
代码实现:
int[] arr = {24, 69, 80, 57, 13};
for(int x = 0; x < arr.length-1; x++){
for(int i = 0; i < arr.length-x; i++){
if(arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
Arrays 类的概述和常用方法
方法名 | 说明 |
---|---|
public static String toString(int[] a) | 返回指定数组的内容的字符串表示形式 |
public static void sort(int[] a) | 按照数字顺序排列指定的数组 |
工具类的设计思想:
- 构造方法用private修饰
- 成员用public static 修饰
基本类型包装类 概述
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Int和String类型的相互转换
// int --> String
int number = 100;
方式一: String s1 = "" + number;
方式二: String s2 = String.valueof(number);
// String --> int
String s = "100";
方式一: int x = Integer.valueof(s).intValue();
方式二: int y = Integer.parseInt(s);
时间相关的类
Data: 关键方法有无参构造方法和带参构造方法(参数为long类型). 时间的初始值为1970年1月1号 00:00:00.
SimpleDataFormat:
// date转换为String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(date);
// String转换为Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(s);
Calender:
// 使用日历类获取时间
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
方法名 | 说明 |
---|---|
public int get(int field) | 返回给定日历字段的值 |
public abstract void add(int field, int amount) | 根据日历的规则, 将指定的时间量添加或减去给定的日历字段 |
public final void set(int year, int month, int date) | 设置当前日历的年月日 |
异常类 的学习