java常用类库

StringBuffer类

package cn.a.stringbuffer;
//StringBuffer类方便用户频繁进行字符串内容的修改

public class TestDemoA {

	public static void main(String[] args) {
		
		//StringBuffer buf = new StringBuffer();
		//buf.append("www").append(" Hello");//利用public StringBuffer append(数据类型 变量)方法连接数据
		//System.out.println(buf);
		//String类和StringBuffer类都是接口CharSequence的子类,后者可以利用对象自动向上转型进行接口实例化
		//CharSequence buf = "wwww";
		//System.out.println(buf);
		//String和StringBuffer类相互转换主要用
		//StringBuffer类的构造函数和append方法public StringBuffer(String str) public StringBuffer append(String str)
		//String类 利用toString()转换String str = buf.toString() public String(StringBuffer sb)
		//StringBuffer类的几个常用方法append(),reverse(),insert(int offset,数类,变量),delete(开始,结束);返回值都是StrinfBuffer
		//buf.append() buf.reverse() buf.insert(0,"www") buf.delete(0,5);

	}

}

jvm进程中运行时的操作类

package cn.b.runtime;
//jvm进程中运行时的操作类对象
//使用了单例设计模式
public class TestDemoB {

	public static void main(String[] args) {
		
		/*Runtime run = Runtime.getRuntime();//单例设计模式调用类内定义的方法实例化对象
		System.out.println(run.maxMemory());
		System.out.println(run.totalMemory());
		System.out.println(run.freeMemory());*/
		//public void gc(),垃圾收集器,释放垃圾空间
		/*Runtime run = Runtime.getRuntime();
		String str = "";
		for (int x = 0; x < 2000; x++) {
			str += x; //大量垃圾
		}
		System.out.println("垃圾处器前");
		System.out.println(run.maxMemory());
		System.out.println(run.totalMemory());
		System.out.println(run.freeMemory());
		run.gc();
		System.out.println("垃圾处器后");
		System.out.println(run.maxMemory());
		System.out.println(run.totalMemory());
		System.out.println(run.freeMemory());*/
		
		/*Runtime run = Runtime.getRuntime();
		Process pro = run.exec("mspaint.exe");
		Thread.sleep(2000);
		pro.destroy();*/
		
		

	}

}

System类

package cn.c.system;

public class TestDemoC {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//数组复制
		System.arraycopy(src, srcPos, dest, destPos, length);
		//取得当前日期时间
		System.currentTimeMillis();
		//long start;long end;end-start
		System.gc();//垃圾回收
		//protect void finalize()方法表示对象回收前的收尾工作,此方法即使错误也不影响程序执行
		
	}

}

对象的克隆

package cn.d.clone;
//对象的克隆
//protect Object clone() throws
class Book implements Cloneable {
	//实现此接口证明可以被克隆
	private String title;
	private double price;
	public Book(String title, double price) {
		super();
		this.title = title;
		this.price = price;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [title=" + title + ", price=" + price + "]";
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	
}
public class TestDemoD {

	public static void main(String[] args) throws Exception {
		
		Book bookA = new Book("yuwen",10.2);
		Book bookB = (Book)bookA.clone();//向下强制转型
		//a和b是两个不同的存储单元,设置互不影响
		System.out.println(bookA);
		System.out.println(bookB);

	}

}

数字操作类

package cn.e.math;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;

class MyMath {
	public static double round(double num, int scale) {
		BigDecimal big = new BigDecimal(num);
		BigDecimal result = big.divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP);
		return result.doubleValue();
	}
}

public class TestDemoE  {

	public static void main(String[] args) throws Exception {
		
		//System.out.println(Math.round(11.5));
		//System.out.println(Math.round(-11.5));
		//随机数类Random
		/*Random rand = new Random();
		for (int x = 0; x < 20; x++) {
			System.out.print(rand.nextInt(100) + ",");
		}*/
		//大数字操作类
		/*BigInteger numA = new BigInteger("23256984566222");
		BigInteger numB = new BigInteger("23588899966654");
		System.out.println("加法结果=" + numA.add(numB));
		System.out.println("减法结果=" + numA.subtract(numB));
		System.out.println("乘法结果=" + numA.multiply(numB));
		System.out.println("除法结果=" + numA.divide(numB));
		BigInteger [] result = numA.divideAndRemainder(numB);
		System.out.println("有余数的除法结果=" + "商: " + result[0]
							+ " 余数: " + result[1]);*/
		
		System.out.println(MyMath.round(168.98165, 2));
		
		

	}

}

date与calender类

package cn.f.date;

import java.util.Calendar;

public class TestDemoF {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		/*Date date = new Date();
		System.out.println(date);*/
		/*long cur = System.currentTimeMillis();
		Date date = new Date(cur);
		System.out.println(date);
		System.out.println(date.getTime());*/
		/*Date date = new Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
		String str = sd.format(date);
		System.out.println(str);*/
		/*String str = "2012-09-03 11:20:10.6565";
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
		Date date = sd.parse(str);
		System.out.println(date);*/
		Calendar cal = Calendar.getInstance();
		StringBuffer buf = new StringBuffer();
		buf.append(cal.get(Calendar.YEAR)).append("-");
		buf.append(cal.get(Calendar.MONDAY)).append("-");
		buf.append(cal.get(Calendar.DAY_OF_MONTH)).append(" ");
		buf.append(cal.get(Calendar.HOUR_OF_DAY)).append(":");
		buf.append(cal.get(Calendar.MINUTE)).append(":");
		buf.append(cal.get(Calendar.SECOND));
		System.out.println(buf);
	}

}

Arrays类

package cn.g.arrays;

import java.util.Arrays;

public class TestDemoG {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*//二分查找
		int data[] = new int [] {9,5,6,3,4,1,2,7,8};
		java.util.Arrays.sort(data);
		System.out.println(Arrays.binarySearch(data, 3));*/
		/*//数组相等
		int dataA[] = new int[] {2,3,4};
		int dataB[] = new int[] {2,3,4};
		System.out.println(Arrays.equals(dataA, dataB));*/
		//数组填充
		int data[] = new int[10];
		Arrays.fill(data, 7);
		System.out.println(Arrays.toString(data));
	}

}

comparable对象数组排序

package cn.h.comparable;

import java.util.Arrays;

class Book implements Comparable<Book> {
	private String title;
	private double price;
	public Book(String title, double price) {
		super();
		this.title = title;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [title=" + title + ", price=" + price + "]\n";
	}
	@Override
	public int compareTo(Book o) {
		// TODO Auto-generated method stub
		if (this.price > o.price) {
			return 1;   //Arrays.sort()会自动调用此方法进行比较
		} else if (this.price < o.price) {
			return -1;
		} else {
			return 0;
		}
	}
	
}

public class TestDemoH {

	public static void main(String[] args) throws Exception {
		// 对象数组排序实现Comparable接口
		Book books[] = new Book[] {new Book("A",1.25),
									new Book("B",1.26),
									new Book("C",1.24)};
		Arrays.sort(books);
		System.out.println(Arrays.toString(books));
		
	}

}

compatator

package cn.i.comparator;

import java.util.Arrays;


class Book {
	private String title;
	private double price;
	public Book() {
	}
	public Book(String title, double price) {
		this.title = title;
		this.price = price;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [title=" + title + ", price=" + price + "]\n";
	}
	
}
class BookComparator implements java.util.Comparator<Book> {

	@Override
	public int compare(Book o1, Book o2) {
		// TODO Auto-generated method stub
		if (o1.getPrice() > o2.getPrice()) {
			return 1;
		} else if (o1.getPrice() < o2.getPrice()) {
			return -1;
		} else {
			return 0;
		}
	}
	
}

public class TestDemoI {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Book books[] = new Book[] {new Book("A",1.25),
				new Book("B",1.26),
				new Book("C",1.24)};
		Arrays.sort(books, new BookComparator());
		System.out.println(Arrays.toString(books));
	}

}

正则表达式

 package cn.j.regex;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


//正则表达式方便进行字符串复杂操作
public class TestDemoJ {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		/*//实现字符串的替换
		String str = "Whello*()((4worl*90d";
		String regex = "[^a-z]";
		System.out.println(str.replaceAll(regex, ""));*/
		/*//字符串的拆分
		String str = "nba999msdv5522263A989B";
		String regex = "\\d+";
		String result[] = str.split(regex);
		for (int x = 0; x < result.length; x++) {
			System.out.println(result[x]);
		}*/
		/*//验证一个字符串是否为数字(小数或者整数),如果是转换为double型
		String str = "10.02";
		String regex = "\\d+(\\.\\d+)?";
		if (str.matches(regex)) {
			System.out.println(Double.parseDouble(str));
		}*/
		/*//192.168.1.1每一段包含1到3位
		String str = "192.168.1.1";
		String regex = "(\\d{1,3}\\.){3}\\d{1,3}";
		System.out.println(str.matches(regex));*/
		/*//判断座机号码010-51283346
		String str = "(010)-51283346";
		String regex = "((\\d{3,4}-)|(\\(\\d{3,4}\\)-))?\\d{7,8}";
		System.out.println(str.matches(regex));*/
		//一段字符串判断日期格式,是就转换成date
		/*String str = "2024-09-24";
		String regex = "\\d{4}-\\d{2}-\\d{2}";
		if (str.matches(regex)) {
			Date date = new SimpleDateFormat("yyyy-MM-dd").parse(str);
			System.out.println(date);	
		}*/
		/*//email地址格式认证
		String str = "mldn_lixinghua100@yootk.com";
		String regex = "[a-zA-Z][a-zA-Z0-9_\\.]{0,28}[a-zA-Z0-9]"
				+"@\\w+\\.(com|cn)";
		System.out.println(str.matches(regex));*/
		//Pattern p = Pattern.compile(regex);数据类型  变量=p.solit(str)
	}

}

reflect反射

package cn.l.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/*class Book {
	public Book() {
		System.out.println("无参构造的实现");
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "A书";
	}
	
}

public class TestDemoI {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		//class类是反射操作的源头类
		//设置类名称实例化class类对象,传递一个反射操作类的完整名称
		Class<?> cls = Class.forName("cn.l.reflect.Book");
		//反射实例化包装类,相当于用new调用无参构造
		Object obj = cls.newInstance(); //反射实例化对象
		Book book = (Book) obj;
		System.out.println(book);
	}

}*/


/*interface Fruit {
	public abstract void eat();
}
class Apple implements Fruit {
	public void eat() {
		System.out.println("吃苹果");
	}
}
class Orange implements Fruit {
	public void eat() {
		System.out.println("吃橙子");
	}
}
//设计一个中间工厂类
//客户端只取得父类接口下的子类对象
//程序的改变不影响客户端的使用
//客户端不必关注细节
class Factory {
	public static Fruit getInstance(String className) {
		Fruit f = null;
		try {
			f = (Fruit)Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return  f;
	}
}

public class  TestDemoI {
	public static void main(String[] args) {
		Fruit f = Factory.getInstance("cn.l.reflect.Apple");
		f.eat();
	}
}*/

/*class Book {
	private String title;
	private double price;
	public Book(String title, double price) {
		this.title = title;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [title=" + title + ", price=" + price + "]";
	}
}
public class TestDemoI {
	public static void main(String args[]) throws Exception {
		Class<?> cls = Class.forName("cn.l.reflect.Book");
		Constructor<?> con = cls.getConstructor(String.class,double.class);//反射调用构造
		Object obj = con.newInstance("A",12.35);
		System.out.println(obj);
	}
}*/

/*class Book{
	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
public class TestDemoI {
	public static void main(String args[]) throws Exception {
		String fieldName = "title";
		Class<?> cls = Class.forName("cn.l.reflect.Book");
		Object obj = cls.newInstance();
		Method setMet = cls.getMethod("set"+initcap(fieldName), String.class);
		Method getMet = cls.getMethod("get"+initcap(fieldName));//反射调用方法
		setMet.invoke(obj, "A书");
		getMet.invoke(obj);
		
	}
	public static String initcap(String str) {
		return str.substring(0, 1).toUpperCase()+str.substring(1);
	}
}*/

class Book {
	private String title;//反射调用成员
}
public class TestDemoI {
	public static void main(String args[]) throws Exception {
		Class<?> cls = Class.forName("cn.l.reflect.Book");
		Object obj = cls.newInstance();
		Field titleField = cls.getDeclaredField("title");//取得类中的属性
		titleField.setAccessible(true);//取消封装
		titleField.set(obj, "A书");
		System.out.println(titleField.get(obj));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值