字符串缓冲类StringBuffer & System系

作者: 奋斗的老王 | 来源:发表于2017-02-08 15:25 被阅读70次

    字符串缓冲类StringBuffer

    • 如果需要频繁修改字符串的内容,建议使用字符串缓冲类StringBuffer

    • StringBuffer 其实就是一个存储字符的容器

      • 增加
        • append(boolean b) 可以添加任意类型 的数据到容器中
        • insert(int offset, boolean b) 指定插入的索引值,插入对应 的内容。
      • 删除
        • delete(int start, int end) 根据指定的开始与结束的索引值删除对应的内容。
        • deleteCharAt(int index) 根据指定 的索引值删除一个字符。
      • 修改
        • replace(int start, int end, String str) 根据指定 的开始与结束索引值替代成指定的内容
        • reverse() 翻转字符串缓冲类的内容。 abc--->cba
        • setCharAt(int index, char ch) 把指定索引值的字符替换指定的字符。
        • substring(int start, int end) 根据指定的索引值截取子串。
        • ensureCapacity(int minimumCapacity) 指定StringBuffer内部的字符数组长度的
      • 查看
        • indexOf(String str, int fromIndex) 查找指定的字符串第一次出现的索引值,并且指定开始查找的位置
        • lastIndexOf(String str)
        • capacity() 查看当前字符数组的长度
        • length()
        • charAt(int index)
        • toString() 把字符串缓冲类的内容转成字符串返回。
    • 笔试题目:使用Stringbuffer无参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍?
      StringBuffer 底层是依赖了一个字符数组才能存储字符数据 的,该字符串数组默认 的初始容量是16, 如果字符数组的长度不够使用 死,自动增长1倍

    • StringBuffer 与 StringBuilder的相同处与不同处:

      • 相同点:
        1. 两个类都是字符串缓冲类
        2. 两个类的方法都是一致的
      • 不同点:
        1. StringBuffer是线程安全的,操作效率低 ,StringBuilder是线程非安全的,操作效率高
        2. StringBuffer是jdk1.0出现 的,StringBuilder 是jdk1.5的时候出现的
      • 推荐使用: StringBuilder,因为操作效率高
    public class Demo {
        
        public static void main(String[] args) {
            //先使用StringBuffer无参的构造函数创建一个字符串缓冲类。
            StringBuffer sb = new StringBuffer(); 
            sb.append("abcjavaabc");
            /*
            添加 
            sb.append(true);
            sb.append(3.14f);
            插入
            sb.insert(2, "小明");
            */
            
            /*
            删除
            sb.delete(2, 4); //  删除的时候也是包头不包尾
            sb.deleteCharAt(3); //根据指定 的索引值删除一个字符
            
            修改  
            sb.replace(2, 4, "陈小狗");
            
            sb.reverse(); // 翻转字符串的内容
            
            sb.setCharAt(3, '红');
            
            String subString = sb.substring(2, 4);
            System.out.println("子串的内容:"+ subString);
            
            查看
        
            int index = sb.indexOf("abc", 3);
            System.out.println("索引值为:"+index);
                
            sb.append("javajava");
            System.out.println("查看字符数组的长度:"+ sb.capacity());
            */
            
            System.out.println("存储的字符个数:"+sb.length());
            System.out.println("索引指定的索引值查找字符:"+sb.charAt(2) );
            System.out.println("字符串缓冲类的内容:"+ sb);
            
            String content = sb.toString();
            test(content);
        }
        
        public static void test(String str){
        }
    }
    
    

    System系统类

    • 主要用于获取系统的属性数据
    • System类常用的方法:
      • arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 一般
      • src : 源数组。
      • srcPos : 源数组中的起始位置。
      • dest : 目标数组。
      • destPos : 目标数据中的起始位置。
      • length : 要复制的数组元素的数量。
      • currentTimeMillis() : 获取当前系统系统(重点)
      • exit(int status) : 退出jvm如果参数是0表示正常退出jvm,非0表示异常退出jvm
      • gc() : 建议jvm赶快启动垃圾回收期回收垃圾。
      • getenv(String name) : 根据环境变量的名字获取环境变量。
      • getProperty(key) :
      • finalize() : 如果一个对象被垃圾回收 器回收的时候,会先调用对象的finalize()方法
    class Person{
        
        String name;
    
        public Person(String name) {
            this.name = name;
        }
        
        @Override
        public void finalize() throws Throwable {
            super.finalize();
            System.out.println(this.name+"被回收了..");
        }
    }
    
    
    
    public class Demo {
        
        public static void main(String[] args) {
            /*
            int[] srcArr = {10,12,14,16,19};
            //把srcArr的数组元素拷贝 到destArr数组中。
            int[] destArr = new int[4];
            
            System.arraycopy(srcArr, 1, destArr, 0,4);
            //System.exit(0); //jvm退出..  注意: 0或者非0的 数据都可以退出jvm。对于用户而言没有任何区别。
            System.out.println("目标数组的元素:"+ Arrays.toString(destArr)); // 0 14 16 0
            System.out.println("当前的系统时间:" + System.currentTimeMillis());
            System.out.println("环境变量:"+System.getenv("JAVA_HOME"));
            
            
            for(int i = 0 ; i<4; i++){
                new Person("狗娃"+i);
                System.gc(); //建议马上启动垃圾回收期
            }
            
            Properties properties = System.getProperties();  //获取系统的所有属性。
            properties.list(System.out);
            */
            String value = System.getProperty("os.name");//根据系统的属性名获取对应的属性值
            System.out.println("当前系统:"+value);
        }
    }
    

    RunTime(该类主要代表了应用程序运行的环境)

    • getRuntime() : 返回当前应用程序的运行环境对象
    • exec(String command) : 根据指定的路径执行对应的可执行文件
    • freeMemory() : 返回 Java 虚拟机中的空闲内存量(以字节为单位)
    • maxMemory() : 返回 Java 虚拟机试图使用的最大内存量
    • totalMemory() : 返回 Java 虚拟机中的内存总量
    public class Demo {
    
        public static void main(String[] args) throws IOException, InterruptedException {
            Runtime runtime = Runtime.getRuntime();
    //      Process process = runtime.exec("C:\\Windows\\notepad.exe");
    //      Thread.sleep(3000); //让当前程序停止3秒。
    //      process.destroy();
            System.out.println(" Java虚拟机中的空闲内存量。"+runtime.freeMemory());
            System.out.println("Java 虚拟机试图使用的最大内存量:"+ runtime.maxMemory());
            System.out.println("返回 Java 虚拟机中的内存总量:"+ runtime.totalMemory());
        }
    }
    

    Date日期类

    • Calendar
    • 日期格式化类 SimpleDateFormat
    public class Demo  {
        
        public static void main(String[] args) throws ParseException {
            /*
                    Date date = new Date(); // 获取当前的系统时间
            System.out.println("年份:"+ date.getYear());
                    */
            /*
            Calendar calendar = Calendar.getInstance(); //获取当前的系统时间。
            System.out.println("年:"+ calendar.get(Calendar.YEAR));
            System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1));
            System.out.println("日:"+ calendar.get(Calendar.DATE));
            
            System.out.println("时:"+ calendar.get(Calendar.HOUR_OF_DAY));
            System.out.println("分:"+ calendar.get(Calendar.MINUTE));
            System.out.println("秒:"+ calendar.get(Calendar.SECOND));
            
            // 显示 当前系统时间: 2014年12月26日  xx时xx分xx秒   
            
             *  日期格式化类    SimpleDateFormat 
             *          作用1: 可以把日期转换转指定格式的字符串     format()
             *          作用2: 可以把一个 字符转换成对应的日期。    parse()   生日
             *      
             */
            Date date = new Date(); //获取当前的系统时间。
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss") ; //使用了默认的格式创建了一个日期格式化对象。
            String time = dateFormat.format(date);  //可以把日期转换转指定格式的字符串
            System.out.println("当前的系统时间:"+ time);
            
            String birthday = "2000年12月26日   11:29:08";
            Date date2 = dateFormat.parse(birthday);  //注意: 指定的字符串格式必须要与SimpleDateFormat的模式要一致。
            System.out.println(date2);
            
            Date date21 =new Date();
            SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
            String time2 =dateFormat.format(date21);
            String time21=dateFormat.format(date);
            System.out.println("当前的系统时间:"+time);
            String birthday1= "2000年12月26日  11:28:08";
            Date date22=dateFormat.parse(birthday1);
            System.out.println(date22);
        }
    }
    

    Math(数学类)

    • 主要是提供了很多的数学公式
      • abs(double a) : 获取绝对值
      • ceil(double a) : 向上取整
      • floor(double a) : 向下取整
      • round(float a) : 四舍五入
      • random() : 产生一个随机数, 大于等于0.0且小于1.0 的伪随机double值
    public class Demo{
        public static void main(String[] args) {
            System.out.println("绝对值:"+Math.abs(-3));
            System.out.println("向上取整:"+Math.ceil(3.14));
            System.out.println("向下取整:"+Math.floor(-3.14)); //
            System.out.println("四舍五入:"+Math.round(3.54));
            System.out.println("随机数:"+Math.random());   
        }   
    }
    
    • 需求: 编写一个函数随机产生四位的验证码
    public class Demo {
    
        public static void main(String[] args) {
            /*
            Random random = new Random();
            int randomNum = random.nextInt(10)+1; //产生 的 随机数就是0-10之间
            System.out.println("随机数:"+ randomNum);
            */
            char[] arr = {'中','国','传','a','Q','f','B'};
            StringBuilder sb = new StringBuilder();
            Random random = new Random();
            //需要四个随机数,通过随机数获取字符数组中的字符,
            for(int i  = 0 ; i< 4 ; i++){
                int index = random.nextInt(arr.length);  //产生的 随机数必须是数组的索引值范围之内的。
                sb.append(arr[index]);
            }
            System.out.println("验证码:"+ sb); 
        }
    }
    

    相关文章

      网友评论

        本文标题:字符串缓冲类StringBuffer & System系

        本文链接:https://www.haomeiwen.com/subject/meovittx.html