常用类

常用类

包装类

1、包装类作为和基本数据类型对应的类型存在,方便涉及到对象的操作。

作用:提供了基本数据类型、字符串、包装类之间的转换。

// 基本类型转化成Integer对象
Integer int1 = new Integer(10);
Integer int2 = Integer.valueOf(20); // 官方推荐这种写法
// Integer对象转化成int
int a = int1.intValue();
/ 字符串转化成Integer对象
Integer int3 = Integer.parseInt("334");
Integer int4 = new Integer("999");
// Integer对象转化成字符串
String str1 = int3.toString();
// 一些常见int类型相关的常量
System.out.println("int能表示的最大整数:" + Integer.MAX_VALUE);

2、自动装箱与拆箱

自动装箱和拆箱就是将基本数据类型和包装类之间进行自动的互相转换。

Integer b=23;// 自动装箱
int a=new Integer(20)//自动拆箱

String

1、String是不可变对象

因为源码加了final。

字符串比较用equal方法,不用==

2、StringBuilder(可变)线程不安全,效率高,一般使用它。

StringBuffer线程安全,效率低

String类方法:

创建并返回一个新的String对象: concat()、 replace()、substring()、 toLowerCase()、 toUpperCase()、trim()。

提供查找功能的有关方法: endsWith()、 startsWith()、 indexOf()、lastIndexOf()。

提供比较功能的方法: equals()、equalsIgnoreCase()、compareTo()。

其它方法: charAt() 、length()。

StringBuilder类方法:

append()、delete(int start,int end)、deleteCharAt(index)、insert()、reverse()

3、

循环累加用StringBuilder。

对String字符串的操作实际上是对其副本(原始拷贝)的操作,原来的字符串一点都没有改变。

如:String s =“a”; 创建了一个字符串

s = s+“b”; 实际上原来的"a"字符串对象已经丢弃了,现在又产生了另一个字符串s+“b”(也就是"ab")。

String str=""
for (int i = 0; i < 5000; i++) {
            str = str + i;//相当于产生了10000个对象
}
//应使用
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < 5000; i++) {
            sb.append(i);
}

时间类

1、Data类

1970 年 1 月 1 日 00:00:00定为基准时间,每个度量单位是毫秒(1秒的千分之一).

方法:

long now = System.currentTimeMillis();

after、before、equals、getTime()……

2、DateFormat

在java.text.DateFormat里。

时间对象和字符串之间的互相转化,按一定的格式把时间对象转化成相应的字符串,或者反向转化。

SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 将时间对象转换成字符串
String daytime = s1.format(new Date());
System.out.println(daytime);
// 将符合指定格式的字符串转成成时间对象.字符串格式需要和指定格式一致。
String time = "2007-10-7";
Date date = s2.parse(time);
System.out.println("date1: " + date);
//今年的多少天
SimpleDateFormat s1 = new SimpleDateFormat("D");
String daytime = s1.format(new Date());
System.out.println(daytime);

3、Calendar

GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。

Calendar是一个抽象类,GregorianCalendar是一个具体子类。

int year=calendar.get(Calendar.YEAR);//DAY_OF_MONTH;DAY_OF_WEEK
//设置日期;week:1-7日一二三四五六
calendar.set(Calendar.day)
calendar.add(Calendar.MONTH,-7)//月份减7

Math

abs,sin\cos……,sqrt,pow,max,min,ceil向上取整,floor向下取整,random,round,toDegrees,tpRadians

random:rand.nextDouble(),rand.nextInt(), 20+ rand.nextInt(10)//[20,30)

File

文件属性:

表8-3 File类访问属性的方法列表.png

通过File对象创建空文件或目录

表8-4 File类创建文件或目录的方法列表.png

使用递归算法,以树状结构展示目录树

static void printFile(File file,int level) {
		for(int i=0;i<level;i++) {
			System.out.print("-");
		}
		System.out.println(file.getName());
		if(file.isDirectory()) {
			File[] files=file.listFiles();
			for(File temp:files) {
				printFile(temp,level+1);
			}
		}
}
#Main
File f=new File("d:/电影");
printFile(f,0);

枚举

枚举就是放置一些常量。

尽量不要使用枚举的高级特性

enum Season {
  SPRING, SUMMER, AUTUMN, WINDER 
}

			}
		}
}
#Main
File f=new File("d:/电影");
printFile(f,0);

枚举

枚举就是放置一些常量。

尽量不要使用枚举的高级特性

enum Season {
  SPRING, SUMMER, AUTUMN, WINDER 
}

参考:

https://www.sxt.cn/Java_jQuery_in_action/eight-timeprocessing.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值