常用类

来源:互联网 发布:淘宝助理上传中断 编辑:程序博客网 时间:2024/05/16 04:33

BigDecimal类,主要适用于精确计算浮点数:
看代码注释:

package BigDecimal;import java.math.BigDecimal;public class Test01 {    //精确表示计算浮点数    public static void main(String[] args) {        System.out.println("0.05+0.01 = "+(0.05+0.01));//不能精确表示浮点数,引发精度度丢失        BigDecimal b = new BigDecimal("0.777");        System.out.println(b);   //通过字符串传参的构造函数可以出现理想得到的        BigDecimal b1 = new BigDecimal(0.1);        System.out.println(b1);      //无法准确表示double型        BigDecimal b2 = BigDecimal.valueOf(0.1);//调用静态类把一个double的值返回给b2        System.out.println(b2);                   BigDecimal f1 = new BigDecimal("0.05");        BigDecimal f2 = new BigDecimal(0.05);        BigDecimal f3 = BigDecimal.valueOf(0.01); //调用静态方法        System.out.println("使用String作为BigDecimal的构造器参数");        System.out.println("0.01+0.05 = "+f1.add(f3));        System.out.println("0.01+0.05 = "+f1.subtract(f3));        System.out.println("0.01+0.05 = "+f1.multiply(f3));        System.out.println("0.01+0.05 = "+f1.divide(f3));        System.out.println("使用double作为BigDecimal的构造器参数");        System.out.println("0.01+0.05 = "+f2.add(f3));        System.out.println("0.01+0.05 = "+f2.subtract(f3));        System.out.println("0.01+0.05 = "+f2.multiply(f3));        System.out.println("0.01+0.05 = "+f2.divide(f3));    }}

Random类:

package random;import java.util.Arrays;import java.util.Random;/** * 通过Random类我们可以给它的实例传入一个随机种子生成一个随机数 * 它有两种构造器,一种是以传入一个long的参数作为种子,一种是以当前时间作为种子 * 和Math中的random()方法区别在于更加丰富,毕竟自成一类 * 相同的种子所生成的伪随机数是相同的,为了避免两个对象出现相同的伪随机数,我们就用以当前时间作为种子 *  * */public class RandomTest {    public static void main(String[] args) {        Random rand = new Random();//允许不传入随机种子        System.out.println(rand.nextBoolean());        System.out.println(rand.nextDouble());        byte[] buffer = new byte[16];        rand.nextBytes(buffer);  //随机产生数值/** *    public static String toString(byte[] a) {  //源码 *      if (a == null) *           return "null"; *       int iMax = a.length - 1; *       if (iMax == -1) *           return "[]";                         *  *      for (int i = 0; ; i++) { *          b.append(a[i]); *         if (i == iMax) *            return b.append(']').toString(); *       b.append(", "); *  } */        System.out.println(Arrays.toString(buffer));//若空返回null字符串,否则返回扩容后的字符串StringBuilder定义        Random ran1 = new Random();        System.out.println(ran1.nextInt(10));//生成0到10的伪随机数        Random r2 = new Random(10);//传入的参数仅仅只是代表一个随机种子        System.out.println(r2.nextInt());        System.out.println(r2.nextBoolean());        Random ran2 = new Random(System.currentTimeMillis());//以当前时间作为种子        System.out.println(ran2.nextBoolean());        System.out.println(ran2.nextDouble());        System.out.println(ran2.nextFloat());        System.out.println(ran2.nextGaussian());        System.out.println(ran2.nextInt());        System.out.println(ran2.nextInt(6));        System.out.println(ran2.nextLong());        //System.out.println(ran2.nextBytes(buffer));    }}

克隆函数:

package clone;/** * clone * @author lenovo * */class Dog {    String name;       int age;       public Dog(String name,int age){           this.name = name;           this.age = age;       }}class CloneDog implements Cloneable {    Dog dog;    String color;    public CloneDog(String color) {        //super(_name,_age)        this.color = color;        dog = new Dog("旺财",4);    }    public CloneDog clone()throws CloneNotSupportedException{        //源代码中丢出异常,我们这里直接给上级        return (CloneDog)super.clone();//重写克隆函数,强制转化为当前克隆狗    }}public class CloneTest {    public static void main(String[] args)throws CloneNotSupportedException {        CloneDog c1 = new CloneDog("withe");        System.out.println(c1.color+" "+c1.dog.name+" "+c1.dog.age);        CloneDog c2 = c1.clone();        System.out.println(c2.color+" "+c2.dog.name+" "+c2.dog.age);        System.out.println(c1==c2);//false    }}

String类:

package array;public class ArrayTest2 {    public static void main(String[] args) {        String s1 = new String();//传入的是一个空字符序列        System.out.println(s1);//并不是返回null        String s2 = new String("abcdefghijklmnopqrstuvwxyz");//传入一组不可变字符序列        System.out.println(s2);        System.out.println();        char[] ch = new char[3];        for(int i=0;i<3;i++) {            ch[i] = 'i';        }        String s3 = new String(ch);//传入的字符数组被copy到了不可变数组里        System.out.println(s3);        System.out.println(ch.length);//length是数组类里面的final成员变量,一旦赋值就无法改变        System.out.println(s3.length());  //return array.length        System.out.println(s2.equals(s3));// equals()被重写,比较字符串序列本身而不是地址        s2.toCharArray();        System.out.println(s3);        System.out.println(s3.valueOf(5)); //通过调用toString()返回一个整型包装类对象值,跟传入参数有关,传入什么类型的参数返回与之相关的包装类        System.out.println(s3);        System.out.println(s2.charAt(2));  //返回数组序列中的定位的字符    }

StringBuilder类

package StringBuilder;public class Test01 {    public static void main(String[] args) {        StringBuilder s1 = new StringBuilder();//默认16个单元字符序列        s1.append("abcdefghijklmnopqrstuvwxyz");//继承父类的扩容的方法,超过16个字符序列自动扩容        s1.append("true").append(23455); //通过return this 实现方法         System.out.println(s1);        System.out.println(s1.length());        s1.delete(1,6); //删除        System.out.println(s1);        s1.deleteCharAt(2);        System.out.println(s1);        StringBuilder s2 = new StringBuilder(32);        System.out.println(s2.length());    }}
0 0
原创粉丝点击