首页 > 数据库 > 数据库开发技术 > 常用 API 算法介绍
Math
Math 包含基本数字运算方法
public static int abs (int a) 绝对值
public static double ceil (double a) 向上取整
public static double floor (double a) 向下取整
public static int round (float a) 四舍五入
public static int max (int a,int b) 返回最大值
public static int min (int a,int b) 返回最小值
public static double pow(double a,double b)
返回 a 的 b 次幂的值
public static double random () 生成[0,1) 的随机数
System: 包含几个有用类的字段和方法,不能被事例化
public static void exit (int states) 终止当前运行的 Java 虚拟机,非零表示异常终止
public static long currentTimemillis () 获取当前时间,以毫秒为单位
Object
Object 是类层次结构的根,每个类都可以将 Object 类作为超类。所有类都直接或间接的继承此类
构造方法:public void Object ()
回想面向对象对象,为什么子类默认访问父类无参构造方法?因为他们的顶层只有一个无参构造方法
常用方法:
public String toString () 返回对象字符串表现形式。建议所有子类覆盖此方法,自动生成
public boolean equals (Object o) 比较对象是否相等,默认比较地址,重写比较内容,自动生成
Ctrl+B : 查看源代码
Arrays
排序:将数据按照固定的规则进行排列
冒泡排序:一种排序方式,对要排序的数据相邻两个两两进行比较,较大的放在后面,依次对数据进行操作,直到所有数据按要求排序完成
如果有 n 个数据总共需要比较 n-1 次
每次比较数据完毕,下次就会少一个数据参与比较
核心代码
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-i;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=arr[j];
}
}
}
Arrays 类:包含操作数组的各种方法
常见方法:
public static String toString (int [] a) 返回指定数组的字符串表示形式
public static void sort (int a) 按照数字顺序形式排列数组
工具类的设计思想
构造方法用 private 修饰
成员用 public static 修饰
基本类型的包装类
概述:将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作数据
常用的操作之一:用于基本数据类型和字符串之间的转换
基本数据类型 包装类型
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double
boolean Boolean
Integer 概述:包装一个原始类型 int 的值
常用方法:
public Integer (int a) 根据 int 值创建 Integer 对象,已过时
public Integer (String a) 根据 String 值创建 Integer 对象,已过时
public static Integer valueOf (int a) 反正指定类型值 int 的 Integer 实例
public static Integer valueOf (String a) 返回保存指定值的 Integer 对象 String
int 转换为 String
public static String valueOf (int a) 返回 int 类型的字符串表现形式,该方法是 String 类的方法
String 转换为 int 类型
public static int parseInt (String a) 将字符串解析为 int 类型,该方法是 Integer 类的方法
自动装箱和拆箱
装箱:是把基本数据类型转换为对应包装类类型
拆箱:是把包装类类型转换为对应的基本数据类型
Integer i=100;// 自动装箱
i += 200; //i = i + 200; i+200 自动拆箱 i=i+200 自动装箱
注意:在使用包装类类型,如果操作,最好先判断是否为空
电商 API 接口: 电商 API 数据 企业级数据
Date
Date 类概述和构造方法
Date 代表了一个特定的时间,准确到毫秒
public Date () 分配一个 Date 对象,并初始化,以便它代表它被分配的时间,准确到毫秒
public Date (long date) 分配一个 Date 对象,并将其初始化为表示从表中基准时间起指定的毫秒数
常用方法
public long getTime () 获取的是日期对象从 1970 年 1 月 1 日 00:00:00 到现在的毫秒数
public void setTime (long time) 设置时间,给的是毫秒值
SimpleDateFormat 类概述
SimpleDateFormat 是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。我们重点学校日期格式化和解析
日期和时间格式由日期和时间模式字符串指定,在日期和时间模式字符串中,从 'A' 到 'Z' 以及从 'a' 到 'z' 引号的字母被解释为
表示日期或时间字符串的组件的模式字母
长运的模式字母对应关系如下
y 年
M 月
d 日
H 时
m 分
s 秒
SimpleDateFormat 构造方法
public SimpleDateFormat () 构造一个 SimpleDateFormat,使用默认模式和日期格式
public SimpleDateFormat (String pattern) 构造一个 SimpleDateFormat 使用给定的模式和默认的日期格式
SimpleDateFormat 格式化和解析日期
1 格式化(从 Date 到 String)
public final String format (Date date): 将日期格式化成日期 / 时间字符串
2 解析(从 String 到 Date)
public Date parse (String source): 从给定字符串的开始解析文本以生成日期
Calendar
Calendar 为某一时刻和一组日历字段的转换提供了一些方法,并未操作日历字段提供了以下方法
Calendar 提供了一个类方法 getInstance 用于获取 Calendar 对象,其日历自动已使用当前日期和时间初始化;
Calendar rightNow=Calendar.getInstance();
Calendar 常用方法
public int get (int field) 返回给定日历字段的值
public abstact void add (int field,int amount) 根据日历规则,将指定时间量添加或减去给定的日历字段
public final void set (int year,int month,int date) 设置当前日历的年月日
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70026917/viewspace-2938472/ ,如需转载,请注明出处,否则将追究法律责任。