大数据开发笔记day13

大数据开发笔记day13

Java语言基础(主要是JavaSE部分)


今日所学的知识点:

一.常用类之String类
这里写图片描述
1.String类定义:字符串是一个比较特殊的对象,可以使用new,也可以不使用new来创建字符串对象
String s1 = new String(“abc”);
String str = “abc”;
两者的区别:前者在堆中,后者在常量池中
(常量池:方法区的一部分,字符串在常量池里面保存一份)
2.String类的特点:字符串是不可变量,一旦初始化就不可以被改变
String s1 = “abc”;
s1 = s1 + “123”;
//s1变量变了,并不是字符串变了
这里写图片描述

面试题:以下语句创建了几个对象?
String str1 = new String("abc");
答:一个或者两个:
如果之前常量池里有“abc”了,就在堆中创建一个对象;
如果常量池里没有“abc”对象,则先在常量池中把”abc”创建出来,然后再到堆空间中创建对象.
package com.oldboyedu.test03;
public class StringDemo {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = "abc";
        String s3 = "abc";
        System.out.println(s1==s2);//false
        System.out.println(s2==s3);//true
    }
}

3.String类的常用构造方法

#1.使用构造方法创建的对象都是在堆中
#2.如果直接使用字符串常量创建对象:
new String(String original)先在常量池中创建对象,然后在堆中创建对象;
#3.如果是其它构造方法创建的字符串,先在堆中创建出来,并在常量池保存一个副本(如果常量池中没有的话)

①String(char[] ch)
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        char[] chs = {'a','b','c','d','e','f','g'};
        String str = new String(chs);//使用字符数组构建字符串对象
        System.out.println(str);
    }
}
运行结果是:abcdefg

②String(char[] ch,int offset,int count)
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        char[] chs = {'a','b','c','d','e','f','g'};
        String str = new String(chs,2,3);//使用字符数组的一部分构建对象
        System.out.println(str);
    }
}
运行结果是:cde

③String(byte[] bys,int off,int len)
//使用默认的解码,将字节数组解码成String
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        //使用字节数组构建字符串
        byte[] arr = {97,98,99,100,101};
        //使用字节数组的一部分构建字符串
        String str = new String(arr,2,3);
        System.out.println(str);
    }
}
运行结果是:cde

④String(String original)
//用一个原始字符串构建一个新字符串
这里写图片描述
4.String类的常用成员方法
charAt(int index);//统计大小写字符个数
这里写图片描述

package com.oldboyedu.test03;
public class StringDemo {
    public static void main(String[] args) {
        String str = "qwertyuiop[]asdfghjkl;'ABCDEFG中文";
        int lower = 0;
        int upper = 0;
        int other = 0;
        for(int i = 0;i<str.length();i++){
            char ch = str.charAt(i);
            //判断
            if(ch >= 'a' && ch <= 'z'){
                lower++;
            }else if(ch >= 'A' && ch <= 'Z'){
                upper++;
            }else{
                other++;
            }
        }
        System.out.println("小写字符个数: " + lower);
        System.out.println("大写字符个数: " + upper);
        System.out.println("其它字符个数: " + other);
    }
}
运行结果是:小写字符个数: 19
            大写字符个数: 7
            其它字符个数: 6

②length();//统计字符串的长度
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        //返回字符串长度(字符个数)
        String s = "abc中文";
        System.out.println(s.length());
    }
}
运行结果是:5

toCharArray();//使用此方法改写统计大小写字符个数
这里写图片描述

package com.oldboyedu.test03;
public class StringDemo2 {
    public static void main(String[] args) {
        //toCharArray()
        String str = "qwertyuiop[]asdfghjkl;'ABCDEFG中文";
        //自动补齐方法的返回值类型和变量名
        char[] chs = str.toCharArray();
        int lower = 0;
        int upper = 0;
        int other = 0;
        for (int i = 0; i < chs.length; i++) {
            if(chs[i] >= 'a' && chs[i] <= 'z'){
                lower++;
            }else if(chs[i] >= 'A' && chs[i] <= 'Z'){
                upper++;
            }else{
                other++;
            }
        }
        System.out.println("小写字符个数: " + lower);
        System.out.println("大写字符个数: " + upper);
        System.out.println("其它字符个数: " + other);
    }
}
运行结果是:小写字符个数: 19
            大写字符个数: 7
            其它字符个数: 6

④indexOf(String str);
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        //indexOf(String str) 
        String str = "acbcdeabcab"; 
        int i =str.indexOf("ab"); 
        System.out.println(i);
    }
}
运行结果是:6

startsWith(String str);
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        // startsWith(String str)
        String str = "acbcdeabcab"; 
        boolean bol = str.startsWith("ac");
        System.out.println(bol);
    }
}
运行结果是:true

⑥endsWith(String str);
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        // endsWith(String str)
        String str = "acbcdeabcab"; 
        boolean bol = str.endsWith("cab");
        System.out.println(bol);
    }
}
运行结果是:true

split(String reg);//统计单词
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        // split(String str)
        String str = "hello world nihao hehe welcome nice to meet you";
        String[] split = str.split(" ");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }
    }
}
运行结果是:hello
            world
            nihao
            hehe
            welcome
            nice
            to
            meet
            you
package com.oldboyedu.test03;
public class StringDemo3 {
    public static void main(String[] args) {
        String str2 = "The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run."
                + "This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. "
                + "Being active simply means that a thread has been started and has not yet been stopped. "
                + "In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by "
                + "instantiating a Thread instance and passing itself in as the target."
                + "In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes "
                + "should not be subclassed unless the programmer intends on modifying or "
                + "enhancing the fundamental behavior of the class.";
        String[] words = str2.split(" ");
        int count = 0;
        for (int i = 0; i < words.length; i++) {
            if(words[i].length() == 4){
                System.out.println(words[i]);
                count++;
            }
        }
        System.out.println("长度为4的字符串的个数是:"+count+"个。");
    }
}
运行结果是:must
            that
            wish
            code
            they
            that
            been
            been
            that
            most
            used
            only
            This
            长度为4的字符串的个数是:13个。

⑧substring(int beginIndex);
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        // substring(int beginIndex)
        String str = "abcdefghijk";
        String s = str.substring(1);
        System.out.println(s);
    }
}
运行结果是:bcdefghijk

⑨substring(int beginIndex,int endIndex);
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        // substring(int beginIndex)
        String str = "abcdefghijk";
        String s = str.substring(2, 6);//[2,6)
        System.out.println(s);
    }
}
运行结果是:cdef

⑩equals(Object anObject);
这里写图片描述

public class StringDemo {
    public static void main(String[] args) {
        //equals(Object anObject);
        String str2 = "abc";
        String str3 = new String("abc");
        System.out.println(str2 == str3);
        System.out.println(str2.equals(str2));
    }   
}
运行结果是:false
            true
注意:== 和 equals方法的区别(可查看源码)
==:对于引用数据类型,比较的是地址值
equals方法:先比较地址值,若地址不同,再比较字符序列

课后练习题:
短串在长串中出现的次数
例:
String src = “abc111ababb”;
求”ab”出现的次数
(提示:使用startsWith方法)

阅读更多

没有更多推荐了,返回首页