刘意JavaSE 学习笔记 Day12-Day14——字符串API(String/StringBuffer/StringBuilder...)

本文深入探讨Java中字符串(String)、StringBuilder、StringBuffer的使用技巧及区别,讲解字符串构造方法、常用API如equals与==的对比,以及字符串拼接、转换等功能。同时,文章覆盖数组的高级操作,包括冒泡排序、选择排序和二分查找算法的实现。

4【重点,常用API】字符串API(String/StringBuffer/StringBuilder...)

day12 String类(案例做一遍)

day13 StringBuffer

 

5 常用类(Math/System/BigDecimal/日期类)

day14 其他常用类

以上花3天时间

 

第九,第十天

字符串(最常见的对象,没有之一):就是由多个字符组成的一串数据。它可以和字符数组进行相互转换。

字符串的特点:字符串是常量,他们的值(注意,是内容!不是引用不能改,引用是有可能改的)创建后不能更改。字符串字面值“abc”也可看成是一个字符串对象。

构造方法:
        A:public String()        空构造
        B:public String(byte[] bytes)        把字节数组转成字符串
        C:public String(byte[] bytes,int offset,int length)        把字节数组的一部分转成字符串
        D:public String(char[] value)        把字符数组转成字符串
        E:public String(char[] value,int offset,int count)        把字符数组的一部分转成字符串
        F:public String(String original)        把字符串常量值转成字符串【不常用】

字符串的常用方法: public int length()     返回此字符串的长度

字面值作为字符串对象和通过构造方法创建对象的不同
            String s = new String("hello");和String s = "hello";的区别?

前者创建两个对象,后者只创建一个对象。

注:==比较的是地址值是否相同,equals()经过String重写后比较的是对象的成员变量值是否相同

字符串的面试题(看程序写结果)
        A:==和equals()
            String s1 = new String("hello");
            String s2 = new String("hello");
            System.out.println(s1 == s2);// false
            System.out.println(s1.equals(s2));// true

            String s3 = new String("hello");
            String s4 = "hello";
            System.out.println(s3 == s4);// false
            System.out.println(s3.equals(s4));// true

            String s5 = "hello";
            String s6 = "hello";
            System.out.println(s5 == s6);// true
            System.out.println(s5.equals(s6));// true
        B:字符串的拼接
            String s1 = "hello";
            String s2 = "world";
            String s3 = "helloworld";
            System.out.println(s3 == s1 + s2);// false【变量相加,先开空间,再拼接】
            System.out.println(s3.equals((s1 + s2)));// true

            System.out.println(s3 == "hello" + "world");// 应该是true【常量相加,先加后在常量池找,有就返回没有就创建】
            System.out.println(s3.equals("hello" + "world"));// true

字符串的功能(方法):

A:判断功能
            boolean equals(Object obj)          比较字符串的内容是否相同
            boolean equalsIgnoreCase(String str)        比较字符串的内容是否相同(忽略大小写)
            boolean contains(String str)        判断大字符串中是否包含小字符串
            boolean startsWith(String str)        判断字符串是否以某个指定的字符串开头
            boolean endsWith(String str)        判断字符串是否以某个指定的字符串结尾
            boolean isEmpty()        判断字符串内容是否为空(String s = ""; 字符串内容为空    String s = null; 字符串对象不存在!)

例子:登录猜数字游戏

B:获取功能
            int length()        获取字符串的长度
            char charAt(int index)        获取指定索引位置的字符
            int indexOf(int ch)        返回指定字符在此字符串中第一次出现处的索引【不是char类型因为'a'和97是一样的】
            int indexOf(String str)        返回指定字符串在此字符串中第一次出现处的索引【一般是该字符串的首字母】
            int indexOf(int ch, int fromIndex)        返回指定字符在此字符串中从指定位置后第一次出现处的索引
            int indexOf(String str, int fromIndex)        返回指定字符串在此字符串中从指定位置后第一次出现处的索引
            String substring(int start)        从指定位置开始截取字符串,默认至末尾
            String substring(int start, int end)        从指定位置开始到指定位置结束截取字符串
               

C:转换功能
            byte[] getBytes()        把字符串转换为字节数组
            char[] toCharArray()        把字符串转换为字符数组
            static String valueOf(char[] chs)        把字符数组转换成字符串【括号里是个数组】
            static String valueOf(int i)        把int类型的数据转换成字符串
            String toLowerCase()        把字符串转成小写
            String toUpperCase()        把字符串转成大写
            String concat(String str)        把字符串拼接【直接 s1 + s2更加直观方便】
        

D:其他功能
            a:替换功能 
                String replace(char old,char new)       
                String replace(String old,String new)       
            b:去空格功能(两端空格,中间如果有空格不管)
                String trim()       
            c:按字典比较功能【重点掌握】 相同输入0 不同输出差值
                int compareTo(String str)       
                int compareToIgnoreCase(String str)        

 

第十一天

StringBuffer的构造方法
        A:StringBuffer()
        B:StringBuffer(int size)
        C:StringBuffer(String str)

数组高级

(1)排序

A:冒泡排序
            相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。同理,其他的元素就可以排好。

 public static void bubbleSort(int[] arr) {
                for(int x=0; x<arr.length-1; x++) {
                    for(int y=0; y<arr.length-1-x; y++) {
                        if(arr[y] > arr[y+1]) {
                            int temp = arr[y];
                            arr[y] = arr[y+1];
                            arr[y+1] = temp;
                        }
                    }
                }
            }

  B:选择排序
            把0索引的元素,和索引1以后的元素都进行比较,第一次完毕,最小值出现在了0索引。同理,其他的元素就可以排好。

public static void selectSort(int[] arr) {
                for(int x=0; x<arr.length-1; x++) {
                    for(int y=x+1; y<arr.length; y++) {
                        if(arr[y] < arr[x]) {
                            int temp = arr[x];
                            arr[x] = arr[y];
                            arr[y] = temp;
                        }
                    }
                }
            }


    (2)查找
        二分查找(折半查找)
            针对数组有序的情况(千万不要先排序,在查找)

 public static int binarySearch(int[] arr,int value) {
                int min = 0;
                int max = arr.length-1;
                int mid = (min+max)/2;
                
                while(arr[mid] != value) {
                    if(arr[mid] > value) {
                        max = mid - 1;
                    }else if(arr[mid] < value) {
                        min = mid + 1;
                    }
                    
                    if(min > max) {
                        return -1;
                    }
                    
                    mid = (min+max)/2;
                }
                
                return mid;
            }

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值