JAVA学习笔记-----Twelve(StringBuffer,Arrays,Integer)

一.StringBuffer类

1.A:StringBuffer类概述
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。
而StringBuffer就可以解决这个问题
线程安全的可变字符序列
2.StringBuffer类的构造方法
public StringBuffer(): 无参构造方法
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值
3. StringBuffer的添加功能
public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
4.StringBuffer的删除功能
public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
5.StringBuffer的替换和反转功能
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
public StringBuffer reverse(): 字符串反转
6.StringBuffer的截取功能及注意事项
A:StringBuffer的截取功能
public String substring(int start): 从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
B:注意事项
注意:返回值类型不再是StringBuffer本身
7.StringBuffer和String的相互转换
A:String – StringBuffer
a:通过构造方法
b:通过append()方法
B:StringBuffer – String
a: 使用substring方法
b:通过构造方法
c:通过toString()方法
8.StringBuffer和StringBuilder的区别
A:形式参数问题
String作为参数传递 String虽然是引用类型,但是它是一个常量,所以在做传递的时候,完全可以将其看成基本数据类型数据进行传递
StringBuffer作为参数传递
案例演示:

//容器中的字符序列也是有索引的从0开始
        StringBuffer sb = new StringBuffer();
        sb.append("abc").append(true).append(new char[]{'a', 'b', 'c'});
        String s = sb.toString();
        System.out.println(s);

        //可以往容器中的指定位置插入数据
        sb.insert(2, 100);//在你指定的索引前面插入数据 返回的还是那个容器
        System.out.println(sb);


        //删除容器中的字符
        // sb.deleteCharAt(0);//根据索引删除一个字符
        sb.delete(0, 4);//从0索引处开始,删除4个字符 返回的还是那个容器
        System.out.println(sb);

public static void main(String[] args) {
       // StringBuffer-----String
        //方式1
        StringBuffer sb= new StringBuffer().append(100);
        String s = sb.toString();
        System.out.println(s);
        //方式2
        String s1 = sb.substring(0);
        System.out.println(s1);
    }

 //反转字符串
        String str="abcd";
        String s = new StringBuffer(str).reverse().toString();
        System.out.println(s);

    StringBuffer sb = new StringBuffer("123");
        StringBuffer sb2=set(sb);
        System.out.println(sb.toString());//321
        System.out.println(sb2.toString());//321
        System.out.println(sb2==sb);


二. Arrays类

1.Arrays类的概述和方法使用
A:Arrays类概述
针对数组进行操作的工具类。
提供了排序,查找等功能。
B:成员方法
public static String toString(int[] a)
public static void sort(int[] a)
public static int binarySearch(int[] a,int key)

三.Integer类

1,Integer类概述
通过JDK提供的API,查看Integer类的说明

Integer 类在对象中包装了一个基本类型 int 的值,
该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
还提供了处理 int 类型时非常有用的其他一些常量和方法

a:构造方法
public Integer(int value)
public Integer(String s)
2.String和int类型的相互转换
A:int – String
a:和""进行拼接
b:public static String valueOf(int i)
c:int – Integer – String
d:public static String toString(int i)
B:String – int
a:String – Integer – intValue();
b:public static int parseInt(String s)

案例演示:

//编写数组工具类,完成冒泡排序、选择排序、二分查找,并测试
public class Test {
    public static void main(String[] args) {

        int [] arr={1,3,2,4,5};
        MP mp = new MP();
        mp.MaoPao(arr);
        XZ xz = new XZ();
        xz.Xuanze(arr);
        int [] b={1,2,3,4,5,6};
        EF ef = new EF();
        System.out.println(ef.ErFen(b, 3));
    }
}
class XZ {
     public static void Xuanze(int [] arr){
        for (int i = 0; i <arr.length-1 ; i++) {
            for (int j = i+1; j <arr.length ; j++) {
                if(arr[i]>arr[j]){
                    int t=arr[i];
                    arr[i]=arr[j];
                    arr[j]=t;
                }
            }

        }
         System.out.println(Arrays.toString(arr));
    }
}



class MP {
    public static void  MaoPao(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j]<arr[j+1]){
                    int t=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=t;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}


class EF {
    public static int ErFen(int []arr,int index){
        int min=0;
        int max=arr.length-1;
        int c=(min+max)/2;
        while(min<=max){
            if (arr[c]==index){
                return c;
            }else if (arr[c]<index){
                min=c+1;
            }else if (arr[c]>index){
                max=c-1;
            }
            c=(min+max)/2;
        }
        return -1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值