Java核心编程
常用类
String
是一个不可变的字符串,案例如下
StringBuffer
线程安全,可变的字符序列
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abc");
stringBuffer.append("d");//追加
System.out.println(stringBuffer);
stringBuffer.insert(2,456);//插入元素,插入位置为2
stringBuffer.deleteCharAt(3); //删除下标为3的元素
stringBuffer.delete(1,2);//删除指定字符串
stringBuffer.replace(1,2,"45645");//替换字符串
stringBuffer.setCharAt(6,'='); //修改指定元素
stringBuffer.reverse();//逆转元素
System.out.println(stringBuffer);
}
StringBuilder
非线程安全,可变的字符序列,具体用法与StringBuffer类似
String StringBuffer StringBuilder三者的区别
在这方面运行速度快慢为:StringBuilder > StringBuffer > String
String:适用于少量的字符串操作的情况
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况
Arrays
该类的构造器是私有的,即不能new对象,因为该类中所有的方法都是静态的,所以再调用类中的方法时通过类名.方法名来调用,简单的案例如下:
public static void main(String[] args) {
int [] a ={55,33,66,22,99,11};
System.out.println(Arrays.toString(a)); //输入数组a中的值,转换为字符串
Arrays.sort(a); //对A进行排序,默认为升序排序可以自定义排序
Integer [] b = {55,33,66,22,99,11};
Arrays.sort(b, new Comparator<Integer>() { //自定义降序排序,
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2){
return -1;
}
return 0;
}
});
int index = Arrays.binarySearch(a,66);//在数组a中查找66的位置
int[] c = Arrays.copyOf(a,3); //复制数组a,新数组的长度为3,则只复制前三个元素
int[] d =Arrays.copyOfRange(a,0,5);//复制数组a的指定长度中的元素
Arrays.fill(a,5);//将数组a中所有的元素替换为5
}
Math
数学函数,所有的方法都是静态的,简单案例如下
public static void main(String[] args){
int abs = Math.abs(-15); //取得指定元素的绝对值
double ceil = Math.ceil(4.5);//向上取整 5
double floor = Math.floor(4.5);//向下取整 4
double round=Math.round(4.3);//四舍五入
double min = Math.min(4,6);//取得俩个元素中小的哪一个
double max = Math.max(4,6);//取得俩个元素中大的哪一个
double exp = Math.exp(1);//e的多少次方
double log =Math.log(2);//对数函数
double pow = Math.pow(2,3);//2的3次方
double sqrt = Math.sqrt(4);//对4 进行开方
double random = Math.random();//取0~1之间的一个随机数
while (true){ //摇一次筛子,只能出现1~6
int random1 = (int)(Math.random()*10);
if(random1 <=6){
System.out.println("筛子点数:"+random1);
break;
}
}
}
日期类
Date 时间类
public static void main(String[] args){
Date date = new Date();//创建当前时间,日期对象
System.out.println(date);
long time = date.getTime();//时间戳,在某些时候可以作为唯一标识
System.out.println(time);
time += 24 * 60 * 60 *1000; //为当前时间戳加一天
System.out.println(time);
Date date1 = new Date(time);//将时间戳转为日期
System.out.println(date1);
System.out.println(new Date(0));//Thu Jan 01 08:00:00 CST 1970 起始时间
}
Calendar 日历类
不能new创建新的对象
public static void main(String[] args){
Calendar calendar = Calendar.getInstance(); //日历类
System.out.println(calendar);
System.out.println(calendar.get(Calendar.YEAR)); //获取年
System.out.println(calendar.get(Calendar.MONTH)); //获取月
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); //获取周几
calendar.set(Calendar.YEAR,2020);//对年进行赋值
calendar.add(Calendar.MONTH,-1);//对月份进行操作
}
SimpleDateFormat 线程不安全
It is recommended to create separate format instances for each thread
为每个线程创建一个SimpleDateFormat对象,在使用的时候
public static void main(String[] args){
//Date->String
Date date2 = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//日期格式化
String date_str = sdf1.format(date2);//格式化
System.out.println(date_str);
//String->Data
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Date date3 = sdf2.parse(date_str);/* 将字符串格式转换为时间类型 */
System.out.println(date3);
} catch (ParseException e) {
e.printStackTrace();
}
}
LocalDate
public static void main (String[] args){
//1.8新增日期处理类
LocalDate today = LocalDate.now();//获取当前日期
LocalDate localDate = LocalDate.of(2008,8,8);//设置日期
LocalDate localDate1 = LocalDate.parse("2008-08-08"); //将字符串转换为时间
}