工具类总结
Arrays 数组工具类
@Test
public void test(){
int[] arr ={0,1,2,3,4,5,6,7,8,9};
//打印的是地址
System.out.println(arr);
//打印数组
System.out.println(Arrays.toString(arr));
/**
* 源码
* public static String toString(int[] a) {
* if (a == null)
* return "null";
* int iMax = a.length - 1;
* if (iMax == -1)
* return "[]";
*
* StringBuilder b = new StringBuilder();
* b.append('[');
* for (int i = 0; ; i++) {
* b.append(a[i]);
* if (i == iMax)
* return b.append(']').toString();
* b.append(", ");
* }
* }
*
*/
//排序
Arrays.sort(arr);
//查找下标
System.out.println(Arrays.binarySearch(arr,6));
//数组扩容源码
/**
* public static int[] copyOf(int[] original, int newLength) {
* int[] copy = new int[newLength];
* //见system工具类
* System.arraycopy(original, 0, copy, 0,
* Math.min(original.length, newLength));
* return copy;
* }
*
*/
int[] ints = Arrays.copyOf(arr, 15);
}
Math工具类
@Test
public void test1(){
System.out.println(Math.abs(-100));
System.out.println(Math.min(100,120));
//四舍五入
System.out.println(Math.round(100.5));//100
System.out.println(Math.round(-100.6));//-101
System.out.println(Math.round(-100.5));//-100
//返回小于等于参数的最大整数
System.out.println(Math.floor(3.5)); //3.0
System.out.println(Math.floor(-3.5)); //-4.0
//返回大于等于参数的最大整数
System.out.println(Math.ceil(3.5)); //4.0
System.out.println(Math.ceil(-3.5)); //-3.0
}
Objects 工具类
@Test
public void test1(){
String s1=null;
String s2= "123";
// System.out.println(s1.equals(s2));
/**
* NullPointerException 因为 s1为空 不是一个对象
* 如果s1是外部传过来的参数 就会有异常
*
*/
System.out.println(Objects.equals(s1,s2));//false 源码return (a == b) || (a != null && a.equals(b));
}
String 工具类
@Test
public void test1(){
//字符串保存在字符串常量池里 内容一样 对象相等 new 会新开空间所以不一样
String s1="123";
String s2="123";
String s3=new String("123");
System.out.println(s1==s2);//true
System.out.println(s1==s3);//false
}
@Test
public void test2(){
//这里我们对String‘修改’了 不是不能修改吗?
// 这是因为不能修改的是内容 而我们修改是相当于新建了一块内存 并将s1指向了 这个新的字符串
// 每一次相加都对创建一个新的内存 在吧内存地址赋值给引用对象
String s1="123";
String s2="123";
s1=s1+s2;
System.out.println(s1==s2);//true
}
@Test
public void test3(){
//因为使用String 连接字符串及其占用内存 使用使用下列的类来进行操作
// StringBuilder 线程不安全 StringBuffer 线程安全 其他完全相同
/**
* capacity() 返回当前容量
* charAt(int index) 返回指定索引处的此序列中的 char值。
* indexOf(String str) 返回指定子字符串第一次出现的字符串中的索引。
* insert(int offset, String str) 将 str 参数的字符串插入此序列中。
* public delete(int start, int end) 移除此序列的子字符串中的字符。
*/
StringBuffer stringBuffer=new StringBuffer("开始");
stringBuffer.append("123");
stringBuffer.append("456");
System.out.println(stringBuffer);
}
System工具类
@Test
public void test1(){
System.gc();//调用方法让jvm回收垃圾
}
@Test
public void test2(){
System.exit(0); //0表示正常终止 非0表示非正常终止
}
@Test
public void test3(){
long l = System.currentTimeMillis();//东八区到现在的时间戳
System.out.println(l);
}
@Test
public void test4(){
/**
* public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* src - 源数组。
* srcPos - 源数组中的起始位置。
* dest - 目标数组。
* destPos - 目标数据中的起始位置。
* length - 要复制的数组元素的数量。
*/
int[] a={1,1,2};
int[] b = new int[10];
System.arraycopy(a,0,b,0,a.length);
System.out.println(Arrays.toString(b));//[1, 1, 2, 0, 0, 0, 0, 0, 0, 0]
}
Date工具类
public static void main(String[] args) throws ParseException {
Date date=new Date(); //精确到毫秒, Date对象并将其初始化以表示自标准基准时间(称为“纪元”)以来的指定毫秒数,即1970年1月1日00:00:00 GMT。
System.out.println(date.getTime());//1632212649632 毫秒数
System.out.println(date.getTime()-(24*60*60*1000));//减去一天
/**
* y : 年
* M : 月
* d : 日
* H : (24)时 h: (12)时
* m : 分
* s : 秒
*
*/
//格式化时间戳
//SimpleDateFormat的父类是DateFormat(抽象类),它允许格式化(日期→文本),解析(文本→日期)和规范化。
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//format( 要格式化的对象 ) 按要求格式化
String format1 = simpleDateFormat.format(new Date());
System.out.println(format1);
Date parse = null;
//将时间戳转为一个Date对象
parse = simpleDateFormat.parse("2021-09-21 16:35:03");
System.out.println(parse);//Tue Sep 21 16:35:03 CST 2021
}
@Test
public void Test1(){
//Calendar 抽象日历类 通过Calendar getInstance(Locale aLocale)获取一个Calendar对象
Calendar calendar =Calendar.getInstance();
/**
* 源码
* public int get(int field)
* {
* complete();
* return internalGet(field);
* }
* protected final int internalGet(int field)
* {
* return fields[field];
* }
* Calendar.YEAR 是Calendar类中定义的常量 表示 年月日等在数组中的下标
*/
int i = calendar.get(Calendar.YEAR);
System.out.println(i); //2021
int i1 = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(i1);//265
calendar.set(Calendar.YEAR,2023);
System.out.println(calendar.get(Calendar.YEAR));//2023
//月份 0-11
calendar.add(Calendar.MONTH,4); //当前9月 所以值为8 加4-> 9 10 11 0
System.out.println(calendar.get(Calendar.MONTH)); //0
Date d=calendar.getTime();
System.out.println(d); //Mon Jan 22 11:18:18 CST 2024
//当前月最大天数
int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(actualMaximum);//31
}
BigDecimal(大浮点数)与BigInteger(大整数)工具类
public static void main(String[] args) {
//使用浮点数 运算的时候 会出现误差
System.out.println(0.1+0.2);//0.30000000000000004
/**
* 使用BigDecimal 使用两个BigDecimal 对象运算
* 返回一个BigDecimal类型的返回值
* add() 加法
* subtract() 减法
* multiply() 乘法
* remainder() 取余
* divide() 除法
* pow(); a.pow(b)=a^b
* gcd(); 最大公约数
* abs(); 绝对值
* negate(); 取反数
* mod(); a.mod(b)=a%b=a.remainder(b);
* max(); min();
* punlic int comareTo();
* boolean equals();
*/
BigDecimal b1=new BigDecimal(0.1);
// BigDecimal b1=BigDecimal.valueOf(0.1); //两种方法都可
BigDecimal b2=new BigDecimal(0.2);
BigDecimal b3 = b1.add(b2);
System.out.println(b3); //0.3
System.out.println(b3 instanceof BigDecimal); //true
//转为double类型 其他类型同理
double v = b3.doubleValue();
/**
* 与大浮点数相同的是 大整数 BigInteger 其方法和BigDecimal相同
*
*/
}