StringBuffer、Arrys类和数组排序

在这里插入图片描述

StringBuffer

如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题。
1、String与StringBuffer区别
前者一旦定义不能改变内容和长度,后者可以。
2、StringBuffer与StringBuilder区别
StringBuffer:同步的,数据安全,效率低。
StringBuilder:不同步的,数据不安全,效率高。
3、StringBuffer与数组的区别
StringBuffer存放的数据类型只有字符串,数组可以存放多种类型,但一个数组中只能存放一种。

一、构造方法

 public static void main(String[] args) {
        StringBuffer sb1 = new StringBuffer();
        System.out.println(sb1);//重写了tostring方法
        System.out.println(sb1.capacity());//当前容量(默认值为16)
        System.out.println(sb1.length());//当前长度
        System.out.println("==================");
        StringBuffer sb2 = new StringBuffer(50);
        System.out.println(sb2);
        System.out.println(sb2.capacity());//50
        System.out.println(sb2.length());//0
        System.out.println("==================");
        StringBuffer sb3 = new StringBuffer("hello");
        System.out.println(sb3);
        System.out.println(sb3.capacity());//25
        System.out.println(sb3.length());//5

    }

二、功能方法

2.1、添加功能

public static void main(String[] args) {
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = sb1.append("hello");
        System.out.println(sb1);
        System.out.println(sb2);
        System.out.println(sb1==sb2);//ture(没有重新开空间)
    }
public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("helloworld");
        sb.insert(5,"111111");
        System.out.println(sb);
    }

2.2、删除功能

public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("hello123456");
        sb.delete(5,11);
        System.out.println(sb);//hello
    }

2.3、替换功能

 public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("hellojava");
        sb.replace(5,9,"*");
        System.out.println(sb);

    }

2.4、反转功能

public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("hellojava");
        sb.reverse();
        System.out.println(sb);
    }

三、练习

3.1、String与StringBuffer互相转换

public static void main(String[] args) {
        StringBuffer sb1 = new StringBuffer("helloworld");//把字符串转换成字符串缓冲区(方式一)
        String s="helloworld";
        StringBuffer sb2 = new StringBuffer();
        sb2.append(s);//把字符串转换成字符串缓冲区(方式二)
        System.out.println("======================");
        String s1 = new String(sb1);//把字符串缓冲区转换为字符串(方式一)
        String s2 = sb2.toString();//把字符串缓冲区转换为字符串(方式二)
    }

3.2、把数组拼接成字符串

public static void main(String[] args) {
          int [] arr={1,2,3,4,5};
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if(i==arr.length-1){
                sb.append(arr[i]);
            }else{
                sb.append(arr[i]).append(",");
            }
        }
        sb.append("]");
        System.out.println(sb.toString());
    }

3.3、反转字符串

 public static void main(String[] args) {
        String s="hello";
        StringBuffer sb = new StringBuffer(s);
        System.out.println(sb.reverse().toString());
    }

3.4、传参问题

字符串虽然是引用数据类型,但是作为参数传递时,形参的变化不影响实参。

数组排序

一、排序

1.1、冒泡排序

在这里插入图片描述

public static void main(String[] args) {
        int [] arr={24,69,80,57,13};
        System.out.println("排序前");
        printArray(arr);
        BubbleSort(arr);
        System.out.println("排序后");
        printArray(arr);
    }

    private static int[] BubbleSort(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;
                }
            }
        }
        return arr;
    }

    private static void printArray(int[] arr) {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if(i==arr.length-1){
                sb.append(arr[i]).append("]");
            }else{
                sb.append(arr[i]).append(",");
            }
        }
        System.out.println(sb);
    }

1.2、选择排序

在这里插入图片描述

 public static void main(String[] args) {
        int[] arr = {24, 69, 80, 57, 13};
        System.out.println("排序前");
        printArray(arr);
        SelectionSort(arr);
        System.out.println("排序后");
        printArray(arr);
    }

    private static int [] SelectionSort(int[] arr) {
        for(int i=0;i<arr.length-1;i++){
            for(int j=1+i;j<arr.length;j++){
                if(arr[i]>arr[j]){
                    int t=arr[i];
                    arr[i]=arr[j];
                    arr[j]=t;
                }
            }
        }
        return arr;
    }

    private static void printArray(int[] arr) {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]).append("]");
            } else {
                sb.append(arr[i]).append(",");
            }
        }
        System.out.println(sb);
    }

二、查找

2.1、二分法查找

在这里插入图片描述

public static void main(String[] args) {
        int [] arr={11,22,33,44,55,66,77};
        int value=44;
        int index= BinarySearch(arr,value);
        System.out.println(index);
    }

    private 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 {
                min=mid+1;
            }
            if(min>max){
                return -1;
            }
            mid=(max+min)/2;
        }
        return mid;
    }

Arrays工具类

 public static void main(String[] args) {
        int [] arr={12,65,13,84,65,43};
        System.out.println(Arrays.toString(arr));//以字符串形式打印出来
        Arrays.sort(arr);//排序
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.binarySearch(arr,13));//二分查找
    }

包装类

将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
常用的操作之一:用于基本数据类型与字符串之间的转换。
基本类型和包装类的对应
Byte, Short, Integer ,Long, Float, Double, Character,Boolean

一、Integer类概述及其构造方法

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

构造方法:

public Integer(int value)
public Integer(String s)
 public static void main(String[] args) {
        int i=100;
        Integer integer = new Integer(i);
        System.out.println(integer);
        String str="100";             //必须是数值
        Integer integer1 = new Integer(str);
        System.out.println(integer1);
    }

二、Integer类成员方法

public int intValue()
public static int parseInt(String s)
public static String toString(int i)
public static Integer valueOf(int i)
public static Integer valueOf(String s)

int---------string

public static void main(String[] args) {
        int num=100;
        //方式一
        String str1=""+num;
        //方式二
        String str2=String.valueOf(num);
        //方式三
        //int ---Integer---String
        Integer in = new Integer(num);
        in.toString();
        //方式四
        String str4=Integer.toString(num);
    }

String--------int

public static void main(String[] args) {
        String str="100";
        //方式一
        //String---Integer----int
        Integer in = new Integer(str);
        int i1 = in.intValue();
        //方式二
        int i2 = Integer.parseInt(str);
    }

三、自动拆装箱

JDK1.5以后,简化了定义方式。

Integer x = new Integer(4);//可以直接写成
Integer x = 4;//自动装箱。
x  = x + 5;//自动拆箱。通过intValue方法。

需要注意:
在使用时,Integer x = null;上面的代码就会出现NullPointerException。

自动装箱:把基本类型转换为包装类类型
自动拆箱:把包装类类型转换为基本类型

四、Character类概述及其构造方法

Character 类在对象中包装一个基本类型 char 的值,此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然。

构造方法:

public Character(char value)
public static void main(String[] args) {
        Character ch = new Character('a');
        System.out.println(ch);
    }

五、Character类成员方法

public static boolean isUpperCase(char ch)
public static boolean isLowerCase(char ch)
public static boolean isDigit(char ch)
public static char toUpperCase(char ch)
public static char toLowerCase(char ch)
public static void main(String[] args) {
        char ch='A';
        boolean b1 = Character.isUpperCase(ch);
        boolean b2 = Character.isLowerCase(ch);
        boolean b3 = Character.isDigit(ch);
        char c1 = Character.toLowerCase(ch);
        char c2 = Character.toUpperCase(c1);
    }

统计一个字符串中大写字母,小写字母,数字

public static void main(String[] args) {
        String str="Hello123javaWorld";
        int da=0;
        int xiao=0;
        int num=0;
        for (int i = 0; i < str.length(); i++) {
            if(Character.isUpperCase(str.charAt(i))){
                da++;
            }else if(Character.isLowerCase(str.charAt(i))){
                xiao++;
            }else{
                num++;
            }
        }
        System.out.println("大写字母:"+da+" 小写字母:"+xiao+" 数字:"+num);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值