天天看点

黑马程序员---API常用类及其方法总结

---------------------- 黑马程序员 Android培训、期待与您交流! ---------------------

1:Object类

    (1)是类层次结构的根类,超类,所有的类都直接或者间接的继承自该类。

    (2)成员方法:

        A:toString()

            返回对象的字符串表示。

            默认格式:包名.类名@哈希值的十六进制。

            重写该方法。

        B:getClass()

            返回对象的字节码描述文件。反射时候再讲。

        C:hashCode()

            返回对象的哈希值。可以理解为地址值。

        D:finalize()

            被对象的垃圾回收器调用,用于垃圾回收。

        E:equals()

            默认情况下,比较对象的地址值。

            建议重写该方法。而且,不用自己动手。

            一般重写方法的时候:都是比较的是对象的成员变量值。

    (3)==和equals()的区别?

        A:==

            a:基本类型 比较的是基本类型的值

            b:引用类型 比较的是引用类型的地址值

        B:equals()

            只能比较引用类型。

            默认比较地址值。

2:Scanner

    (1)用于键盘录入的。

    (2)使用步骤:

        A:导包

        B:创建对象

        C:调用方法

    (3)成员方法:

        int nextInt()

        String nextLine()

    (4)小问题:

        int -- int

        String -- String

        String -- int

        int -- String 有问题。

        解决方案:

            A:要么都是字符串,然后要什么转成什么。

            B:重新创建一个新的录入对象。

3:String

    (1)多个字符组成的一串数据。

    (2)构造方法:

    构造函数

        public String() : 空参构造函数

        public String(byte[] bytes) : 把给定的字节数组 转换成字符串

        public String(byte[] bytes,int startIndex,int length) : 把给定的字节数组的一部分,转换成字符串 (包含startIndex位置的元素)

        public String(char[] value) : 把给定的字符数组 转换成字符串

        public String(char[] value,int startIndex,int length) :把给定的字符数组的一部分, 转换成字符串

        public String(String original) : 把字符串 转换成 字符串

    (3)经典题:

        A:字符串一旦被赋值就不能被改变。

            请自己理解这句话。

        B:String s = new String("hello")和String s = "hello";的区别

            前者创建了2个对象

            后者创建了1个对象

        C:看程序写结果

            String s1 = new String("hello");

            String s2 = new String("hello");

            System.out.println(s1==s2);

            System.out.println(s1.equals(s2));

            String s3 = new String("hello");

            String s4 = "hello";

            System.out.println(s3==s4);

            System.out.println(s3.equals(s4));

            String s5 = "hello";

            String s6 = "hello";

            System.out.println(s5==s6);

            System.out.println(s5.equals(s6));

        D:看程序写结果

            String s1 = "hello";

            String s2 = "world";

            String s3 = "helloworld";

            System.out.println(s3==s1+s2);

            System.out.println(s3.equals(s1+s2));

    (4)常见方法:

        A:判断功能

        boolean equals(Object obj) : 把给定的字符串与该字符串比较

        boolean equalsIgnoreCase(String str) : 比较两个字符串是否一致(忽略大小写)

        boolean contains(String str) : 该字符串中 是否包含 给定的字符串

        boolean startsWith(String str) : 判断该字符串是否以 给定的字符串开始

        boolean endsWith(String str) : 判断该字符串是否以 给定的字符串结束

        boolean isEmpty() : 判断字符串是否为空    ("")正确     (null)错误

        B:   获取功能

        int length() : 获取字符串的长度

        char charAt(int index) : 获取该字符串中指定位置对应的字符

        int indexOf(int ch) : 获取给定的字符在该字符串中第一次出现的位置

        int indexOf(String str) : 获取给定的字符串在该字符串中第一次出现的位置

        int indexOf(int ch,int fromIndex): 从指定位置开始, 获取给定的字符在该字符串中第一次出现的位置

        int indexOf(String str,int fromIndex) : 从指定的位置开始, 获取给定的字符串在该字符串中第一次出现的位置

        String substring(int start) : 从指定位置开始, 截取该字符串, 返回一个新的字符串 (包含start位置的元素)

        String substring(int start,int end) : 从指定位置开始, 到指定位置结束,获取该字符串, 返回一个新的字符串(包左不包右)

        C:  转换功能

        byte[] getBytes() : 把该字符串 转换成 字节数组

        char[] toCharArray() : 把该字符串转换成 字符数组

        static String copyValueOf(char[] chs) : 把字符数组 转换成  字符串

        static String valueOf(char[] chs) : 把字符数组 转换成 字符串

        static String valueOf(int i)基本类型 : 把int数据类型的数据 转换成字符串

        String toLowerCase() : 把该字符串 转换成 小写字母

        String toUpperCase() : 把该字符串转换成 大写字母

        String concat(String str) : 把该字符串与给定的字符串连接起来, 返回一个新的字符串  (给定的字符串在后面)

    D:   替换功能

         String replace(char old,char new) : 在该字符串中,使用新的字符 替代老的字符

         String replace(String old,String new) : 在该字符串中, 使用新的字符串 替代 老的字符串

    E:  其他功能

          切割    

              String[] split(String regex)

          去除字符串两端空格    

             String trim()

          按字典顺序比较两个字符串  

             int compareTo(String str)

             int compareToIgnoreCase(String str)

    (5) 举例:

        1, 统计字符串中大写,小写,数字字符个多少个

public class StringDemo {

    public static void main(String[] args) {

        // 键盘录入一个字符串。

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个字符串:");

        String line = sc.nextLine();

        // 定义三个统计变量。

        int bigCount = 0;

        int smallCount = 0;

        int numberCount = 0;

        // 遍历字符串获取到每一个字符。

        for (int x = 0; x < line.length(); x++) {

            char ch = line.charAt(x);

            if (ch >= '0' && ch <= '9') {

                numberCount++;

            } else if (ch >= 'a' && ch <= 'z') {

                smallCount++;  

            } else{

                bigCount++;

            }

        }

        //输出即可。

        System.out.println("大写:"+bigCount);

        System.out.println("小写:"+smallCount);

        System.out.println("数字:"+numberCount);

    }

}

        2, 把字符串的首字母转成大写,其他小写

public class StringTest {

    public static void main(String[] args) {

        // 键盘录入字符串。

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个字符串:");

        String line = sc.nextLine();

        // 截取字符串得到第一个字符的字符串。

        String s1 = line.substring(0, 1);

        // 截取字符串得到除了第一个以后的字符串。

        String s2 = line.substring(1);

        // 把B大写+C小写。

        String s3 = s1.toUpperCase() + s2.toLowerCase();

        System.out.println(s3);

        // 再来一个 链式编程

        // String result = line.substring(0, 1).toUpperCase()

        // .concat(line.substring(1).toLowerCase());

        // System.out.println(result);

    }

}

        3, 查找大串中小串出现的次数

public class StringDemo {

    public static void main(String[] args) {

        // 定义两个字符串

        String maxString = "helloakworakldhahakjavaxixihaakha";

        String minString = "ok";

        // 写一个功能实现。

        int count = getCount(maxString, minString);

        System.out.println(count);

    }

    public static int getCount(String maxString, String minString) {

        int count = 0;

        int index = maxString.indexOf(minString);

        while (index != -1) {

            count++;

            maxString = maxString.substring(index + minString.length());

            index = maxString.indexOf(minString);

        }

        return count;

    }

}

---------------------- 黑马程序员 Android培训、期待与您交流! ---------------------