JAVA基础之StringBuffer类与数组常见操作

1. StringBuffer概念与构造方法

1.1 概念

如果我们要对字符串进行拼接操作,每次拼接都会重新构建一个String对象,会浪费我们很多时间且这样还会浪费空间。

而StringBuffer则解决了这个问题,它包含多个字符的字符串数据,对象是可以扩充和修改的。

1.1.1 StringBuffer与String的区别

String的特点是一旦赋值,便不能更改其指向的字符对象,如果更改。则会指向一个新的字符对象。

StringBuffer是一个具有对象引用传递特点的字符串对象,可以调用其方法动态的进行增减、插入、修改和删除操作,且不用像数组事先定义大小,从而实现多次插入,一次整体取出,所以操作十分灵活方便。

1.2 构造方法

空参构造  public StringBuffer();

指定容量的字符串缓冲区对象a  public StringBuffer(int a);

指定字符串内容的字符串缓冲区对象str public StringBuffer(String str);

1.2.1 StringBuffer的方法

StringBuffer默认容量为16。

public int capacity(); 返回当前容量。      理论值

public int lengh();返回长度(字符数)。         实际值

演示:

package westos;

public class MyTest1{
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer();
        s.append("ssss").append("aaa");
        int a=s.capacity();
        int b=s.length();
        System.out.println(a+"||"+b);
    }
}

结果:

16||7

2. StringBuffer常见功能

2.1 删除功能

public StringBuffer deletecharAt(int index);删除指定位置的字符,并返回本身。

public StringBuffer delete(int strt,int end);删除从指定位置开始到指定位置结束,并返回本身。

演示:

package westos;

public class MyTest2{
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer();
        s.append("abcd").append("efgh");
        System.out.println(s);
        int index = s.indexOf("f");
        s.deleteCharAt(index);
        System.out.println(s);
        int start = s.indexOf("e");
        int end = s.indexOf("h");
        s.delete(start,end);
        System.out.println(s);
    }
}

结果:

abcdefgh
abcdegh
abcdh    //含头不含尾

2.2 替换和反转功能

public StringBuffer replace(int start,int end,String str);从start开始到end用str替换。

public StringBuffer reverse();字符串反转。

演示:

package westos;

public class MyTest3{
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer();
        String str ="sssss";
        s.append("abcd").append("efgh");
        System.out.println(s);
        int start = s.indexOf("e");
        int end = s.indexOf("h");
        s.replace(start,end,str);
        System.out.println(s);
        s.reverse();
        System.out.println(s);
    }
}

结果:

abcdefgh
abcdsssssh
hsssssdcba

2.3 截取功能及其注意事项

public String substring(int start);从指定位置截取到末尾。

public String sybstring(int start,int end);截取从指定位置开始到几位数位置,包括开始,不包括结尾。

注意事项:

返回值类型不再是StringBuffer本身。

演示:

package westos;

public class MyTest4{
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer();
        s.append("abcd").append("efgh");
        System.out.println(s);
        int start = s.indexOf("e");
        int end = s.indexOf("g");
        String str1 = s.substring(start);
        System.out.println(str1);
        System.out.println(s);
        String str2 = s.substring(start,end);
        System.out.println(str2);
        System.out.println(s);
    }
}

结果;

abcdefgh
efgh
abcdefgh
ef
abcdefgh

2.4 StringBuffer和String的相互转换

2.4.1 String——StringBuffer

通过构造方法。

通过append()方法。

演示:

package westos;

public class MyTest5{
    public static void main(String[] args) {
        String str1 = "123456";
        String str2 = "abcdefgh";
        StringBuffer s= new StringBuffer(str2);
        s.append(str1);
        System.out.println(s);
    }
}

结果:

abcdefgh123456

2.4.2 StringBuffer——String

通过substring方法。

通过构造方法。

通过toString()方法。

演示:

package westos;

public class MyTest6{
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer();
        s.append("asdf");
        String str1 = new String(s);
        System.out.println(str1);
        s.append("1234");
        int start = s.indexOf("a");
        int end = s.indexOf("4");
        String str2 = s.substring(start,end+1);
        System.out.println(str2);
        s.append("567890");
        String str3 = s.toString();
        System.out.println(str3);
    }
}

结果:

asdf
asdf1234
asdf1234567890

2.4.3String和StringBuffer分别作为参数传递

a.形式参数问题

String作为参数传递 String虽然是引用类型,但是它是一个常量,所以在做传递的时候,完全可以将其看成基本数据类型数据进行传递。

2.5 StringBuffer和StringBuilder的区别

a.在执行速度方面的比较:StringBuilder > StringBuffer

b.StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操作时,实际上是在一个对象上操作的,不像String一样创建一些对象进行操作,所以速度就快了。

c.StringBuilder:线程非安全的

StringBuffer:线程安全的

d.单线程操作字符串缓冲区 下操作大量数据 = StringBuilder

多线程操作字符串缓冲区 下操作大量数据 = StringBuffer

3. 常见数组应用

3.1 把数组转成字符串

需求:把数组中的数据按照指定格式拼接成一个字符串

例:int[ ] arr = {1,2,3};

输出结果:

"[1,2,3]"

package westos;


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

结果:

[1,2,3]

3.2 字符串反转

需求:把字符串反转

例:键盘录入"abc"

输出结果:"cba"

package westos;

import java.util.Scanner;

public class MyTest8{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入");
        String str = sc.nextLine();
        StringBuffer s= new StringBuffer(str);
        s.reverse();
        System.out.println(s);
    }
}

结果:

请输入
abc
cba

3.3 数组高级冒泡排序

冒泡排序原理:

相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处

package Paixu;

import java.util.Arrays;

public class 冒泡法 {
    public static void main(String[] args) {
        int[] arr={24,69,80,57,13};
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length-1; j++) {
                if(arr[j]>arr[j+1]){
                    int t=arr[j+1];
                    arr[j+1]=arr[j];
                    arr[j]=t;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

结果:

[13, 24, 57, 69, 80]

3.4 数组高级选择排序

选择排序原理:

从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处

package Paixu;

import java.util.Arrays;

public class 选择法 {
    public static void main(String[] args) {
        int[] arr={24, 69, 80, 57, 13};
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if(arr[i]>arr[j]){
                    int t=arr[j];
                    arr[j]=arr[i];
                    arr[i]=t;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

结果:

[13, 24, 57, 69, 80]

4. Arrays类

4.1 Arrays类的概念与方法

4.1.1 概述

针对数组进行操作的工具类。

提供了排序,查找等功能。

4.1.2 方法

成员方法:

public static String toString(int[] a)

public static void sort(int[] a)

public static int binarySearch(int[] a,int key)

4.2 Arrays类的源码解析

a.源码解析:

public static String toString(int[] a)

b.源码解析

public static int binarySearch(int[] a,int key)

4.3 基本类型包装类的概述

a.需求:

将100转换成二进制 , 八进制 , 十六进制

判断一个数是否在int的范围内

b.为什么会有基本类型包装类

为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类型.

c.常用操作:    常用的操作之一:用于基本数据类型与字符串之间的转换。

d.基本类型和包装类的对应

byte             Byte

short            Short

int            Integer

long            Long

float            Float

double                Double

char            Character

boolean                 Boolean

5. Integer类

5.1 Integer类概述和构造方法

5.1.1  Integer类概述

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

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

5.1.2 Integer类构造方法

构造方法:

public Integer(int value)

public Integer(String s)

5.2 Integer的面试题

Integer i1 = new Integer(127);
    Integer i2 = new Integer(127);
    System.out.println(i1 == i2);
    System.out.println(i1.equals(i2));
    System.out.println("-----------");

Integer i3 = new Integer(128);
    Integer i4 = new Integer(128);
    System.out.println(i3 == i4);
    System.out.println(i3.equals(i4));
    System.out.println("-----------");

Integer i5 = 128;
    Integer i6 = 128;
    System.out.println(i5 == i6);
    System.out.println(i5.equals(i6));
    System.out.println("-----------");

Integer i7 = 127;
    Integer i8 = 127;
    System.out.println(i7 == i8);
    System.out.println(i7.equals(i8));
    System.out.println("-----------");

结果:

false

true

------------------

false

true

------------------

false

true

------------------

true

true

5.2String和int类型的相互转换

5.2.1int -- String

和""进行拼接

public static String valueOf(int i)

int -- Integer -- String

public static String toString(int i)

演示:

package westos;

public class MyTest9{
    public static void main(String[] args) {
        int a=100;
        int b =200;
        String str = "";
       str = a+"";
        System.out.println(str);
        Integer i = new Integer(b);
        str = i.toString();
        System.out.println(str);
    }
}

结果:

100

200

5.2.2 String -- int

String -- Integer -- intValue();

public static int parseInt(String s)

演示:

package westos;


public class MyTest10{
    public static void main(String[] args) {
        String str = "1000";
        Integer in = new Integer(str);
        int a = in.intValue();
        System.out.println(a);
    }
}

结果:

1000

6. jdk5的新特性自动装箱和拆箱

6.1JDK5的新特性

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

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

演示

JDK5的新特性自动装箱和拆箱

Integer ii = 100;

ii += 200;

6.2注意事项

在使用时,Integer  x = null;代码就会出现NullPointerException。

个人建议先判断是否为null,然后再使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值