java stringbuffer数组_JavaSE知识-13(StringBuffer&数组排序)

本文详细介绍了Java中的StringBuffer类,包括其构造方法、常用方法如append、insert、delete、replace和reverse等,并通过示例代码进行演示。此外,文章还提及了StringBuffer与StringBuilder的区别以及如何在数组操作和字符串操作中使用StringBuffer。
摘要由CSDN通过智能技术生成

StringBuffer类的概述

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

线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。

StringBuffer类的构造方法

每个字符串缓冲区都有一定的容量。只要字符串缓冲区所包含的字符序列的长度没有超出此容量,就无需分配新的内部缓冲区数组。如果内部缓冲区溢出,则此容量自动增大。从 JDK 5 开始,为该类补充了一个单个线程使用的等价类,即 StringBuilder。与该类相比,通常应该优先使用 StringBuilder 类,因为它支持所有相同的操作,但由于它不执行同步,不安全,所以速度更快。

A:StringBuffer的构造方法:

public StringBuffer():无参构造方法

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

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

B:StringBuffer的方法:

public int capacity():返回当前容量。理论值(不掌握)

public int length():返回长度(字符数)。 实际值

C:案例演示

构造方法和长度方法的使用

package com.hwh.stringbuffer;

public class Demo1_StringBuffer {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer();

System.out.println(sb.length());//0//容器中的字符个数,实际值

System.out.println(sb.capacity());//16//容器的初始容量,理论值

StringBuffer sb2 = new StringBuffer(10);

System.out.println(sb2.length());//0

System.out.println(sb2.capacity());//10

StringBuffer sb3 = new StringBuffer("hwh");

System.out.println(sb3.length());//3//实际字符的个数

System.out.println(sb3.capacity());//19//字符串的length + 初始容量

}

}

StringBuffer的添加功能

StringBuffer的添加功能

public StringBuffer append(String str):

可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身

public StringBuffer insert(int offset,String str):

在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

StringBuffer是字符串缓冲区,当new的时候是在堆内存创建了一个对象,底层是一个长度为16的字符数组

当调用添加的方法时,不会再重新创建对象,在不断向原缓冲区添加字符

package com.hwh.stringbuffer;

public class Demo2_StringBuffer {

public static void main(String[] args) {

demo1();

StringBuffer sb = new StringBuffer("1234");

sb.insert(3, "hwh");//在指定位置添加元素,如果没有指定位置的索引就会报索引越界异常

System.out.println(sb); //123hwh4

}

private static void demo1() {

StringBuffer sb = new StringBuffer();

System.out.println(sb.toString());//

StringBuffer sb2 = sb.append(true);

System.out.println(sb2.toString());//true

StringBuffer sb3 = sb.append("hwh");

System.out.println(sb3.toString());//truehwh

StringBuffer sb4 = sb.append(100);

System.out.println(sb4.toString());//truehwh100

System.out.println(sb.toString()); //truehwh100//StringBuffer类中重写了toString方法,显示的是对象中的属性值

System.out.println(sb2.toString()); //truehwh100

System.out.println(sb3.toString()); //truehwh100

System.out.println(sb4.toString()); //truehwh100

}

}

StringBuffer的删除功能

A:StringBuffer的删除功能

public StringBuffer deleteCharAt(int index):

删除指定位置的字符,并返回本身

public StringBuffer delete(int start,int end):

删除从指定位置开始指定位置结束的内容,并返回本身

package com.hwh.stringbuffer;

public class Demo3_StringBuffer {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer();

//sb.deleteCharAt(5);//当缓冲区中这个索引上没有元素的时候就会报StringIndexOutOfBoundsException

sb.append("hanwenhao");

sb.deleteCharAt(4);//根据索引删除掉索引位置上对应的字符

System.out.println(sb); //hanwnhao

sb.delete(0, 2);//删除的时候是包含头,不包含尾

System.out.println(sb); //nwnhao

sb.delete(0, sb.length());//清空缓冲区

System.out.println(sb);

sb = new StringBuffer();//不要用这种方式清空缓冲区,原来的会变成垃圾,浪费内存

System.out.println(sb);

}

}

StringBuffer的替换和反转功能

A:StringBuffer的替换功能

public StringBuffer replace(int start,int end,String str):

从start开始到end用str替换

B:StringBuffer的反转功能

public StringBuffer reverse():

字符串反转

package com.hwh.stringbuffer;

public class Demo4_StringBuffer {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("hanwenhao");

sb.replace(0, 3, "nameis");//替换

System.out.println(sb); //nameiswenhao

sb.reverse();

System.out.println(sb); //oahnewsieman

}

}

StringBuffer的截取功能及注意事项

A:StringBuffer的截取功能

public String substring(int start):

从指定位置截取到末尾

public String substring(int start,int end):

截取从指定位置开始到结束位置,包括开始位置,不包括结束位置

B:注意事项

注意:返回值类型不再是StringBuffer本身

package com.hwh.stringbuffer;

public class Demo5_StringBuffer {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("woshihwh");

String str = sb.substring(4);

System.out.println(str); //ihwh

System.out.println(sb); //woshihwh

String str1 = sb.substring(4, 7);

System.out.println(str1); //ihw

}

}

StringBuffer和StringBuilder的区别

A:String -- StringBuffer

a:通过构造方法

b:通过append()方法

B:StringBuffer -- String

a:通过构造方法

b:通过toString()方法

c:通过subString(0,length);

package com.hwh.stringbuffer;

public class Demo6_StringBuffer {

public static void main(String[] args) {

demo1();

StringBuffer sb = new StringBuffer("hanwenhao");

String s1 = new String(sb);//通过构造将StringBuffer转换为String

System.out.println(s1);//hanwenhao

String s2 = sb.toString();//通过toString方法将StringBuffer转换为String

System.out.println(s2);//hanwenhao

String s3 = sb.substring(0, sb.length());//通过截取子字符串将StringBuffer转换为String

System.out.println(s3);//hanwenhao

}

private static void demo1() {

StringBuffer sb1 = new StringBuffer("hanwenhao");//通过构造方法将字符串转换为StringBuffer对象

System.out.println(sb1);//hanwenhao

StringBuffer sb2 = new StringBuffer();

sb2.append("hanwenhao");//通过append方法将字符串转换为StringBuffer对象

System.out.println(sb2);//hanwenhao

}

}

把数组转化成字符串

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

举例:

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

输出结果:

"[1, 2, 3]"

用StringBuffer的功能实现

将数组转换为字符串

1,返回值类型String

2,参数列表int[]

package com.hwh.test;

public class Test1 {

public static void main(String[] args) {

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

System.out.println(arrayToString(arr));

}

public static String arrayToString(int[] arr) {

StringBuffer sb = new StringBuffer();//创建字符串缓冲区对象

sb.append("[");//将[添加到缓冲区

//{1,2,3}

for (int i = 0; i < arr.length; i++) {//遍历数组

//sb.append(arr[i] + ", ");//这样做没有]

if(i == arr.length - 1) {

sb.append(arr[i]).append("]");//[1, 2, 3]

}else {

sb.append(arr[i]).append(", ");//[1, 2,

}

}

return sb.toString();

}

}

字符串反转

需求:把字符串反转

举例:键盘录入"abc"

输出结果:"cba"

用StringBuffer的功能实现

将字符串反转

1,返回值类型String

2,参数列表String line

抽取为方法的好处是其他类也可使用方法

package com.hwh.test;

import java.util.Scanner;

public class Test2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);//创建键盘录入对象

String line = sc.nextLine();//将键盘录入的字符串存储在line中

System.out.println(revString(line));

}

public static String revString(String line) {

StringBuffer sb = new StringBuffer(line);//将字符串转换为StringBuffer对象

sb.reverse();//将缓冲区的内容反转

return sb.toString();

}

}

String和StringBuffer分别作为参数传递

A:形式参数问题

String作为参数传递

StringBuffer作为参数传递

B:案例演示

String和StringBuffer分别作为参数传递问题

基本数据类型的值传递,不改变其值

引用数据类型的值传递,改变其值

String类虽然是引用数据类型,但是他当作参数传递时和基本数据类型是一样的

package com.hwh.stringbuffer;

public class Demo7_StringBuffer {

public static void main(String[] args) {

String s = "hwh";

System.out.println(s); //hwh

change(s);

System.out.println(s); //hwh

System.out.println("---------------------");

StringBuffer sb = new StringBuffer();

sb.append("hwh");

System.out.println(sb); //hwh

change(sb);

System.out.println(sb); //hwhitcast

}

public static void change(StringBuffer sb) {

sb.append("itcast");

}

public static void change(String s) {

s += "itcast";

}

}

数组高级冒泡排序代码实现

案例演示

数组高级冒泡排序代码

冒泡排序

1,返回值类型,void

2,参数列表,int[] arr

第一次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3],arr[3]与arr[4]比较4次

第二次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3]比较3次

第三次:arr[0]与arr[1],arr[1]与arr[2]比较2次

第四次:arr[0]与arr[1]比较1次

package com.hwh.array;

public class Demo1_Array {

public static void main(String[] args) {

int arr[] = {24,69,80,57,13};

bubbleSort(arr);

print(arr); //Ctrl + 1 生成新方法

}

public static void print(int[] arr) {

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

}

public static void bubbleSort(int arr[]) {

for (int i = 0; i < arr.length - 1; i++) {//外循环只需要比较arr.length-1次就可以了

for (int j = 0; j < arr.length -i - 1; j++) {//-1为了防止索引越界,-i为了提高效率

if (arr[j] > arr[j+1]) {

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

}

数组高级选择排序代码实现

选择排序

1,返回值类型void

2,参数列表int[] arr

第一次:arr[0]分别与arr[1-4]比较,比较4次

第二次:arr[1]分别与arr[2-4]比较,比较3次

第三次:arr[2]分别与arr[3-4]比较,比较2次

第四次:arr[3]与arr[4]比较,比较1次

package com.hwh.array;

public class Demo2_Array {

public static void main(String[] args) {

int arr[] = {24,69,80,57,13};

selectSort(arr);

print(arr); //Ctrl + 1 生成新方法

}

public static void print(int[] arr) {

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

}

public static void selectSort(int arr[]) {

for (int i = 0; i < arr.length - 1; i++) { //只需要比较arr.length-1次

for (int j = i+1; j < arr.length; j++) {

if (arr[i] > arr[j]) {

swap(arr,i,j);

}

}

}

}

/*

* 换位操作

* 1,返回值类型,void

* 2,参数列表int[] arr.int i,int j

*

* 如果某个方法,只针对本类使用,不想让其他类使用就可以定义成私有的

*/

private static void swap(int[] arr, int i, int j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

数组高级二分查找代码实现及注意事项

案例演示

数组高级二分查找代码

注意事项

如果数组无序,就不能使用二分查找。

因为如果你排序了,但是你排序的时候已经改变了我最原始的元素索引。

package com.hwh.array;

public class Demo3_Array {

public static void main(String[] args) {

int[] arr = {11,22,33,44,55,66,77};

System.out.println(getIndex(arr, 22));

System.out.println(getIndex(arr, 66));

System.out.println(getIndex(arr, 88));

}

/*

* 二分查找

* 1,返回值类型,int

* 2,参数列表int[] arr,int value

*/

public static int getIndex(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) {//当中间值小于了要找的值

min = mid + 1;//最小的索引改变

}else if (arr[mid] > value){//当中间值大于了要找的值

max = mid - 1;//最大的索引改变

}

mid = (min + max) / 2;//无论最大还是最小改变,中间索引都会随之改变

if(min > max) {//如果最小索引大于了最大索引,就没有查找的可能性了

return -1;//返回-1

}

}

return mid;

}

}

运行结果为1,5,-1

Arrays类的概述和方法使用

Arrays类的概述

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

提供了排序, 查找等功能

成员方法

public static String toString(int[] a)

public static void sort(int[] a)

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

package com.hwh.array;

import java.util.Arrays;

public class Demo4_Arrays {

/**

* public static String toString(int[] a)

* public static void sort(int[] a)

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

*

* public static String toString(int[] a) {

if (a == null)//如果传入的数组是null

return "null";//返回null

int iMax = a.length - 1;//iMax最大索引

if (iMax == -1)//如果数组中没有元素

return "[]";//返回[]

StringBuilder b = new StringBuilder();//线程不安全,效率高

b.append('[');//将[添加到字符串缓冲区中

for (int i = 0; ; i++) {//遍历数组,判断语句没有写默认是true

b.append(a[i]);//把第一个元素添加进字符串缓冲区

if (i == iMax)//如果索引等于了最大索引值

return b.append(']').toString();//将]添加到字符串缓冲区,在转换成字符串并返回

b.append(", ");//如果不等于最大索引就将, 添加到缓冲区

}

}

private static int binarySearch0(int[] a, int fromIndex, int toIndex,

int key) {

int low = fromIndex;//最小索引0

int high = toIndex - 1;//最大索引数组长度-1

while (low <= high) {//最小索引小于等于最大索引可以循环判断

int mid = (low + high) >>> 1;//求出中间索引值,(最小+最大)/2

int midVal = a[mid];//通过中间索引获取中间值

if (midVal < key)//中间索引对应的值小于查找的值

low = mid + 1;//最小索引变化

else if (midVal > key)//中间索引对应的值大于查找的值

high = mid - 1;//最大索引变化

else

return mid; // key found//找到了

}

return -(low + 1); // key not found.//-插入点 - 1

}

*/

public static void main(String[] args) {

int[] arr = {33,22,11,44,66,55};

System.out.println(Arrays.toString(arr)); //要导包, 直接调用//数组转字符串

//[33, 22, 11, 44, 66, 55]

Arrays.sort(arr);//排序

System.out.println(Arrays.toString(arr));//[11, 22, 33, 44, 55, 66]

int[] arr2 = {11,22,33,44,55,66};

System.out.println(Arrays.binarySearch(arr2, 22)); //1

System.out.println(Arrays.binarySearch(arr2, 66)); //5

System.out.println(Arrays.binarySearch(arr2, 9));//-插入点-1

}

}

基本类型包装类的概述

A:为什么会有基本类型包装类

将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。

B:常用操作

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

C:基本类型和包装类的对应

byte Byte

shortShort

intInteger

longLong

floatFloat

doubleDouble

charCharacter

booleanBoolean

package com.hwh.wrapclass;

public class Demo1_Integer {

public static void main(String[] args) {

System.out.println(Integer.toBinaryString(60));

System.out.println(Integer.toOctalString(60));

System.out.println(Integer.toHexString(60));

}

}

运行结果为

111100

74

3c

Integer类的概述和构造方法

A:Integer类概述

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

Integer 类在对象中包装了一个基本类型 int 的值,

该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,

还提供了处理 int 类型时非常有用的其他一些常量和方法

B:构造方法

public Integer(int value)

public Integer(String s)

C:案例演示

使用构造方法创建对象

package com.hwh.wrapclass;

public class Demo2_Integer {

public static void main(String[] args) {

System.out.println(Integer.MAX_VALUE);//2147483647

System.out.println(Integer.MIN_VALUE);//-2147483648

Integer i1 = new Integer(100);

System.out.println(i1); //100

//Integer i2 = new Integer("abc");//java.lang.NumberFormatException数字格式异常

//System.out.println(i2);//因为abc不是数字字符串,所以转换会报错

Integer i3 = new Integer("100");

System.out.println(i3); //100

}

}

String和int类型的相互转换

A:int -- String

a:和""进行拼接

b:public static String valueOf(int i)

c:int -- Integer -- String(Integer类的toString方法())

d:public static String toString(int i)(Integer类的静态方法)

B:String -- int

a:String -- Integer -- int

public static int parseInt(String s)

基本数据类型包装类有八种,其中七种都有parseXxx的方法,可以将这七种的字符串表现形式转换成基本数据类型

package com.hwh.wrapclass;

public class Demo3_Integer {

public static void main(String[] args) {

//demo1();

String s1 = "true";

boolean b = Boolean.parseBoolean(s1);

System.out.println(b); //true

//String s2 = "abc";

//char c = Character.p//char的包装类Character中没有pareseXxx的方法

//字符串到字符的转换通过toCharArray()就可以把字符串转换为字符数组

}

private static void demo1() {

//int ----> String int转换成String

int i = 100;

String s1 = i + "";//推荐用

String s2 = String.valueOf(i);//推荐用

Integer i2 = new Integer(i);

String s3 = i2.toString();

String s4 = Integer.toString(i);

System.out.println(s1);//100

System.out.println(s2);//100

System.out.println(s3);//100

System.out.println(s4);//100

//String----> int String 转换int

String s = "200";

Integer i3 = new Integer(s);

int i4 = i3.intValue();//将Integer转换成了int数

System.out.println(i4);//200

int i5 = Integer.parseInt(s);//将String转换为int,推荐用这种

System.out.println(i5);//200

}

}

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

A:JDK5的新特性

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

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

B:案例演示

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

Integer ii = 100;

ii += 200;

C:注意事项

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

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

package com.hwh.wrapclass;

public class Demo4_JDK5 {

public static void main(String[] args) {

//int x = 100;

//Integer i1 = new Integer(x);//将基本数据类型包装成对象,装箱

//

//int y = i1.intValue();//将对象转换为基本数据类型,拆箱

Integer i2 = 100;//自动装箱,把基本数据类型转换成对象

int z = i2 + 200;//自动拆箱,把对象转换为基本数据类型

System.out.println(z); //300

Integer i3 = null;

int a = i3 + 100;//底层用i3调用intValue,但是i3是null,null调用方法就会出现

System.out.println(a);//空指针异常java.lang.NullPointerException

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值