Java应用编程_常用类库汇总1(StringBuffer&CharSequence&对象克隆&数字操作类库&...)

总览

在这里插入图片描述

相关文章链接

【Java应用编程_常用类库汇总2(日期类&正则表达式&国际化程序)】
【Java应用编程_常用类库汇总3(开发支持类库&比较器)】



1,StringBuffer类与CharSquence接口

在这里插入图片描述

demo_StringBuffer类与CharSequence接口

package cn.classes.demo;

public class StringBuffer类与StringBulid类与CharSequence接口 {

	public static void main(String[] args) {
		StringBuffer buf=new StringBuffer("hello world !");
		buf.delete(6, 11).insert(6, "world").append("!").reverse();
		System.out.println(buf);//buf.toString()
		CharSequence str="hello world";
		
		System.out.println((str.subSequence(6, 11)));
		
	}

}

运行结果

!! dlrow olleh
world

Process finished with exit code 0

2,AutoCloseable接口

在这里插入图片描述

demo_AutoCloseable接口

package cn.classes.demo;
interface IMessage extends AutoCloseable{
	public void send();
}
class Message implements IMessage{
	private String mes;

	public Message(String mes) {
		super();
		this.mes = mes;
	}
	public boolean open(){
		System.out.println("[通道打开]");
		return true;
	}
	@Override
	public void send() {
		if(this.open()){
			System.out.println("[发送信息]:"+this.mes);
		}
	}
	@Override
	public void close(){
		System.out.println("[通道关闭]");
	}
}


public class AutoCloseable接口 {
	public static void main(String[] args) {
		try(IMessage mess=new Message("hello")){
			mess.send();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

运行结果

[通道打开]
[发送信息]:hello
[通道关闭]

Process finished with exit code 0

3,Runtime类

在这里插入图片描述

demo_Runtime类

package cn.classes.demo;

public class Runtime{

	public static void main(String[] args) throws Exception {
		Runtime run=Runtime.getRuntime();
		System.out.println(run.availableProcessors()); 
		
		System.out.println("[1]MAX_MEMORY"+run.maxMemory());
		System.out.println("[1]TOTAL_MEMORY"+run.totalMemory());
		System.out.println("[1]FREE_MEMORY"+run.freeMemory());
		
		String str="";
		for (int i = 0; i < 30000; i++) {
			str+=str;
		}
		System.out.println("[2]MAX_MEMORY"+run.maxMemory());
		System.out.println("[2]TOTAL_MEMORY"+run.totalMemory());
		System.out.println("[2]FREE_MEMORY"+run.freeMemory());
		run.gc();
		System.out.println("[3]MAX_MEMORY"+run.maxMemory());
		System.out.println("[3]TOTAL_MEMORY"+run.totalMemory());
		System.out.println("[3]FREE_MEMORY"+run.freeMemory());
	}

}

运行结果(不唯一)

12
[1]MAX_MEMORY2109734912
[1]TOTAL_MEMORY132120576
[1]FREE_MEMORY127959952
[2]MAX_MEMORY2109734912
[2]TOTAL_MEMORY132120576
[2]FREE_MEMORY126372320
[3]MAX_MEMORY2109734912
[3]TOTAL_MEMORY10485760
[3]FREE_MEMORY9178496

Process finished with exit code 0

4,System类

在这里插入图片描述

5,对象克隆

在这里插入图片描述

demo_对象克隆

package cn.classes.demo;
public class 对象克隆 {
	public static void main(String[] args) throws Exception{
		Member memA=new Member("小明",18);
		Member memB=(Member)memA.clone();
		System.out.println(memA);
		System.out.println(memB);
	}
}
class Member implements Cloneable{
	private String name;
	private int age;
	public Member(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "["+super.toString()+"]"+"name="+this.name+"age="+this.age;
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

运行结果(不唯一)

[cn.classes.demo.Member@6e8dacdf]name=小明age=18
[cn.classes.demo.Member@2d554825]name=小明age=18

Process finished with exit code 0

6,数字操作类_Math数学计算类

在这里插入图片描述

demo_Math类

package cn.classes.demo;
class MyRound{
	private MyRound(){}
	/**
	 * 自定义四舍五入方法
	 * @param num 要进行四舍五入的数字
	 * @param scale 四舍五入后需要保存的小数点位数
	 * @return 四舍五入结果
	 */
	public static double round(double num,int scale){
		return Math.round((num*Math.pow(10, scale)))/Math.pow(10, scale);
	}
}
public class Math{
	public static void main(String[] args) {
		System.out.println("绝对值:"+Math.abs(-21));
		System.out.println("最大值:"+Math.max(2.5, 3.3));
		System.out.println("对数:"+Math.log(10));
		System.out.println("15.5四舍五入:"+Math.round(15.5));
		System.out.println("15.51四舍五入:"+Math.round(15.51));
		System.out.println("15.2933的保留两位小数的自定义四舍五入:"+ MyRound_dig.round(15.2953, 2));
		System.out.println("10的2次方:"+Math.pow(10,2));
	}

}

运行结果

绝对值:21
最大值:3.3
对数:2.302585092994046
15.5四舍五入:16
15.51四舍五入:16
15.2933的保留两位小数的自定义四舍五入:15.3
102次方:100.0

Process finished with exit code 0

7,Random随机数类

在这里插入图片描述

demo_Random类

package cn.classes.demo;
import java.util.Random;
public class Random{
	public static void main(String[] args) {
		int data[]=new int[7];
		Random rand=new Random();
		int foot=0;
		while(foot<7){
			int num=rand.nextInt(37);
			if(IsUse(num,data)){
				data[foot++]=num;
			}
		}
		for (int i : data) {
			System.out.print(i+",");
		}

	}
	/**
	 * 判断产生的随机数是否非零且不重复
	 * @param num 产生的随机数
	 * @param temp 接收的数组
	 * @return 不满足条件false 否则 true
	 */
	public static boolean IsUse(int num ,int temp[]){
		if(num==0){
			return false;
		}
		for (int i = 0; i < temp.length; i++) {
			if(num==temp[i]){
				return false;
			}
		}
		return true;

	}

}

运行结果(不唯一)

28,22,35,18,16,3,12,
Process finished with exit code 0

8,大数字处理类

在这里插入图片描述

本文完,欢迎关注我的【Java SE】专栏。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值