加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java--CharAt,StartWith

发布时间:2020-12-15 08:24:56 所属栏目:Java 来源:网络整理
导读:public class CharAtStartWithDemo { public static void main(String[] args){ //jdk8 testCharAt();//1 testStartWith();//4 } public static void testCharAt(){ StringBuffer sb = new StringBuffer(); for (int i = 0; i 1000; i++) { sb.append(i); if
public class CharAtStartWithDemo {
    public static void main(String[] args){
        //jdk8
        testCharAt();//1
        testStartWith();//4
    }
    public static void testCharAt(){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 1000; i++) {
            sb.append(i);
            if (i < 999){
                sb.append(",");
            }
        }
        String s = sb.toString();
        int length = s.length();
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            if (s.charAt(0)=='a'&&s.charAt(1)=='b'&&s.charAt(2)=='c'){}
            if (s.charAt(length-1)=='a'&&s.charAt(length-2)=='b'&&s.charAt(length-3)=='c'){}
        }
        long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }
    public static void testStartWith(){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 1000; i++) {
            sb.append(i);
            if (i < 999){
                sb.append(",");
            }
        }
        String s = sb.toString();
        int length = s.length();
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            if (s.startsWith("abc")){}
            if (s.endsWith("abc")){}
        }
        long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读