当前位置:网站首页>常见的类(了解)
常见的类(了解)
2022-07-26 10:31:00 【一尾流鸢cd】
1、常见类
a、gc():垃圾回收
b、Runtime :gc(),freememory()
关于垃圾收集等的案例
public class RuntimeDemo {
public static void main(String[] args) {
Runtime run= Runtime.getRuntime();
System.out.println("最大内存量:"+run.maxMemory()+"字节");
System.out.println("空闲内存量:"+run.freeMemory()+"字节");
//浪费内存制造垃圾
String str="hello";
for (int i=0;i<1000;i++){
str+=i;
}
System.out.println("浪费内存制造垃圾之后的空闲内存量:"+run.freeMemory()+"字节");
//运行垃圾回收
run.gc();
System.out.println("运行垃圾回收器之后的空闲内存量:"+run.freeMemory()+"字节");
}
}
执行结果:
最大内存量:1862270976字节
空闲内存量:122844720字节
浪费内存制造垃圾之后的空闲内存量:111425072字节
运行垃圾回收器之后的空闲内存量:125426984字节
c、Math:pow(),random(),sqrt(),abs(),floor(),round()
d、Random:产生随机数
import java.util.Random;
public class Demo {
public static void main(String[] args) {
System.out.println(Math.sqrt(9));//开平方
System.out.println(Math.random());//产生随机数
System.out.println(Math.abs(8));//返回绝对值
System.out.println(Math.floor(8.5));//向下取整
System.out.println(Math.round(5.6));//四舍五入
int a=10;
int b=3;
System.out.println(Math.ceil((double)a/b));
System.out.println(Math.pow(2,7));//2的7次方
Random random=new Random();
for(int i=0;i<6;i++){
System.out.println(random.nextInt(50));
}
}
}
System:gc(),currentTimeMillis()
2、操作字符串的类
a、String 类
- 字符串拼接使用“ + ”拼接
- 创建对象的方式有2种: 字符串长度不可变
- 采用直接赋值 String str=“hello”;
- 通过new关键字 String s=new String(“hello”);
- 常用方法
public class StringDemo {
public static void main(String[] args) {
String str="hello 你好 世界!";
//获取所有的子串
String s1[]=str.split(" ");//按空格进行分割
for(int i=0;i<s1.length;i++){
System.out.println(s1[i]);
}
System.out.println("字符串长度:"+str.length());
System.out.println("去掉首位空格后的字符串长度:"+str.trim().length());//去掉首位空格后的字符串长度;
System.out.println("是否以o结尾:"+str.endsWith("o"));
System.out.println("是否以空格开头:"+str.startsWith(" "));
if (str.indexOf("hello")!=-1){
System.out.println("找到hello");
}
System.out.println();
for (int i=0;i<str.length();i++){
System.out.println(str.charAt(i));//根据索引下标找对应字符
}
if(str.equals("hello")){
System.out.println("内容一致");
}
System.out.println(str.concat(" 123")); //字符串拼接
System.out.println(str.toLowerCase());//转小写
System.out.println(str.toUpperCase());//转大写
byte b[]=str.getBytes();//转成字节数组
for(int i=0;i<b.length;i++){
System.out.println(b[i]);
}
int num =123;
String s2=String.valueOf(num);//将字符变为字符串
System.out.println(s2);
//字符串截取
String s3="aaa bbb ccc ddd frty";
String s4=s3.substring(0,3);
System.out.println(s4);
System.out.println(s3.substring(2));//从第几个字符位置开始到结束
//查找并替换
for(int i=0;i<s3.length();i++) {
if (s3.indexOf("a") != -1) {
//查找找到a
s3 = s3.replace('a', 'k');//将a替换为k
}
}
System.out.println(s3);
//set方法 将n变为大写
String methodName="setname";
methodName=methodName.substring(0,1).toUpperCase()+methodName.substring(1);
System.out.println("set"+methodName);
methodName.replace('n','N');
System.out.println(methodName+"()");
}
}
b、StringBuffer
创建对象的方式:StringBuffer buf=new StringBuffer();
长度可变
拼接字符串:append()
常用方法:
public class StringBufferDemo {
public static void fun(StringBuffer b){
b.append("我是").append("String的大哥");
}
public static void main(String[] args) {
StringBuffer buf=new StringBuffer("hello");
buf.append(1);//拼接
buf.append(8.8).append('Q');
buf.append(new Date());//拼接执行代码的时间
fun(buf);//对象
buf.append("还有个兄弟是StringBuffer");
System.out.println(buf);
System.out.println(buf);
buf.reverse();//字符串反转
System.out.println(buf);
buf.insert(0,"first");//在第1个位置添加什么元素
System.out.println(buf);
buf=buf.delete(0,1);//删除第一个元素
System.out.println(buf);
}
}
3、日期类
Date:
SimpleDateFormat:
Calender:
public class DateDemo {
public static void main(String[] args) {
Date date=new Date();//直接实例化输出可得到当前日期
System.out.println(date);//输出的日期不是中国式的
System.out.println(date.getTime()+"毫秒");//1970.1.1 0时0分0秒 距离执行时间有多少毫秒
System.out.println(date.getTime()/1000/60/60/24/365+"年");//换算成年
DateFormat df=DateFormat.getDateInstance();
System.out.println(df.format(new Date()));//输出日期
DateFormat df1=DateFormat.getDateTimeInstance();
System.out.println(df1.format(new Date()));//输出年月日时分秒
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
//固定的表示方式 日期的格式转换
String dtime=sdf.format(date);//
System.out.println(dtime);
String strDate="2021/10/31 0:42:15";
//2021年10月31日 0时42分15秒
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
SimpleDateFormat sdf3=new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
//将日期格式的字符串转换为日期
//parse() 返回值为Date从给定字符串的开始解析文本,已生成一个日期
Date d=null;
try{
d=sdf2.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(sdf2.format(d));
Calendar cal=new GregorianCalendar();
//获取
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.MONTH)+1);
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.HOUR));//12小时制
System.out.println(cal.get(Calendar.HOUR_OF_DAY));//24小时制
System.out.println(cal.get(Calendar.MINUTE));
System.out.println(cal.get(Calendar.SECOND));
System.out.println(cal.get(Calendar.MILLISECOND));//毫秒
cal.clear();
//设置,设置之前要清除
cal.set(Calendar.YEAR,2023);//设置年
//获取设置的年
System.out.println(cal.get(Calendar.YEAR));
}
}
4、总结(主要掌握)
String常用的方法:
字符串长度:str.length(),
去掉首位空格后的字符串长度:str.trim().length()
是否以o结尾:str.endsWith(“o”));
是否以空格开头:str.startsWith(" ");
找到字符串中的hello:str.indexOf(“hello”);
根据索引下标找对应字符:str.charAt(i));
。。。。。。。。。
StringBuffer常用的方法:
拼接字符串:append()
在第1个位置添加什么元素:buf.insert(0,“first”);
删除第一个元素buf=buf.delete(0,1);
。。。。。。。
Date:
1970.1.1 0时0分0秒 距离执行时间有多少毫秒:date.getTime();
。。。。。。。。
SimpleDateFormat:
输出年月日时分秒:(df1.format(new Date()));
日期格式的转换
边栏推荐
猜你喜欢
随机推荐
.NET操作Redis Hash对象
Using native JS to realize custom scroll bar (click to reach, drag to reach)
videojs转canvas暂停、播放、切换视频
我们的Web3创业项目,黄了
上传图片获取宽高
少了个分号
Function templates and non template functions with the same name cannot be overloaded (definition of overloads)
[Halcon vision] software programming ideas
Okaleido ecological core equity Oka, all in fusion mining mode
Comparison of packet capturing tools fiddler and Wireshark
PLC overview
canvas上传图片base64-有裁剪功能-Jcrop.js
面试第二家公司的面试题及答案(二)
Cause: could't make a guess for solution
【Halcon视觉】仿射变换
Navicat15连接本地虚拟机的Mysql(Centos7)
L2-005 set similarity (intersection of vector and set)
[Halcon vision] image gray change
The software cannot be opened
并行、并发及对于高并发优化的几个方向