A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王海平 中级黑马   /  2016-1-28 22:26  /  556 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王海平 于 2016-4-20 09:50 编辑

Scanner
        hasNextInt() :判断接收的值是不是int的类型
        nextInt() 把接收到的int值返回出去
        
        一个小问题:先接收int类型的再用nextLine()接收的时候 会接收不到字符串
                1)再创建一个Scanner对象
                2)用nextLine()来接收数字类型的字符串 然后转成int类型

String
        String str = "abc"; str就是String的一个对象
        字符串一旦被赋值, 值就不能再被改变了
        
        构造方法
                String s1 = "abc";
                String s2 = new String(byte[] bys);
                String s3 = new String(char[] chs);
        String的面试题
                1.判断定义为String类型的s1和s2是否相等
                        * String s1 = "abc";
                        * String s2 = "abc";
                        * System.out.println(s1 == s2); //true                                       
                        * System.out.println(s1.equals(s2));                 
                2.下面这句话在内存中创建了几个对象?
                        * String s1 = new String("abc");/2个                        
                3.判断定义为String类型的s1和s2是否相等
                        * String s1 = new String("abc");                        
                        * String s2 = "abc";
                        * System.out.println(s1 == s2);        //false        
                        * System.out.println(s1.equals(s2));
                4.判断定义为String类型的s1和s2是否相等
                        * String s1 = "a" + "b" + "c";
                        * String s2 = "abc";
                        * System.out.println(s1 == s2);        //true        
                        * System.out.println(s1.equals(s2));
                5.判断定义为String类型的s1和s2是否相等
                        * String s1 = "ab";
                        * String s2 = "abc";
                        * String s3 = s1 + "c";
                        * System.out.println(s3 == s2);//false
                        * System.out.println(s3.equals(s2));
        String的判断功能
                boolean equals(String str);
                boolean equalsIgnoreCase(String str);
                boolean contains(String str);
                boolean startsWith(String str);
                boolean endsWith(String str);
                boolean isEmpty();
               
                案例:登录三次   (equals())
                        
        
        String的获取功能
                int length();
                char charAt(int index);
                int indexOf(int bye);
                int indexOf(String str);
                int indexOf(String str,int fromIndex);
                int lastIndexOf(String str);
                String substring(int start);
                String substring(int start, int end);
                案例:字符串遍历 (lenth() charAt() 或者用 toCharArray()遍历数组)
                案例:大写字母小写字符数字字母出现次数  (需要用到字符串的遍历)
               
        String的转换功能
                byte[] getBytes();
                char[] toCharArray();
                static String valueOf(任意类型);
                String toUpperCase();
                String toLowerCase();
                String concat();
                案例:首字母大写 其余字母小写 (substring() toUpperCase() toLowerCase() concat())
                案例:把数组按照格式转成字符串输出

        String的其他功能
                String replace(char old , char new);
                String replace(String old , String new);
                String trim();去除两端的空格
                int compareTo(String str);
                int compareIgnoreCase(String str);
                案例:字符串反转
                案例:大串中出现小串的次数
        
        String和字符数组的转换
                String --> char[]
                        toCharArray();
                char[] --> String
                        new String(char[] chs);
                        static String valueOf(char[] chs);
        String和字节数组的转换
                String --> byte[]
                        getBytes();
                byte[] -->String
                        new String(byte[] bys);
                        static String valueOf(byte[] bys);

(附加)数据结构的思想:
        拿时间换空间,拿空间换时间

1 个回复

倒序浏览
不错,好好学
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马