当前位置:网站首页>常见的类(了解)
常见的类(了解)
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()));
日期格式的转换
边栏推荐
- 干货likeshop外卖点餐系统开源啦100%开源无加密
- Okaleido ecological core equity Oka, all in fusion mining mode
- About the declaration and definition of template functions [easy to understand]
- 数据库的复习--3.SQL语言
- [Halcon vision] affine transformation
- MLX90640 红外热成像仪测温传感器模块开发笔记(六)红外图像伪彩色编码
- L2-005 set similarity (intersection of vector and set)
- 图片随手机水平移动-陀螺仪。360度设置条件
- [qualcomm][network] QTI service analysis
- 【Halcon视觉】形态学膨胀
猜你喜欢

【Halcon视觉】图像灰度变化

3.1 leetcode daily question 6

MLX90640 红外热成像仪测温传感器模块开发笔记(六)红外图像伪彩色编码

【Halcon视觉】图像的傅里叶变换

Introduction to data analysis | kaggle Titanic mission

Okaleido ecological core equity Oka, all in fusion mining mode

canvas上传图片base64-有裁剪功能-Jcrop.js

Deduct daily question 838 of a certain day

PLC overview

数据库的复习--3.SQL语言
随机推荐
cavans实现静态滚动弹幕
json-c库的简单使用——将json文件转换为struct.
Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
【Halcon视觉】软件编程思路
数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
数据库的复习--3.SQL语言
404页面和路由钩子
【Halcon视觉】形态学腐蚀
【Halcon视觉】图像滤波
[Halcon vision] affine transformation
The reason why go language is particularly slow to develop run and build commands
L2-005 set similarity (intersection of vector and set)
2022pta usual training questions (1-10 string processing questions)
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
Introduction to data analysis | kaggle Titanic mission
【Halcon视觉】图像的傅里叶变换
简单化构造函数的继承方法(二)- ES6中的class继承
Navicat15连接本地虚拟机的Mysql(Centos7)
The difference between equals and = =