[Java语言基础]StringBuffer 、StringBuilder 、数组常见操作、 Arrays 、基本数据类型包装类

StringBuffer

我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。
而StringBuffer就可以解决这个问题
线程安全的可变字符序列

StringBuffer类的构造方法

public StringBuffer():    无参构造方法
 public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
   public StringBuffer(String str):  指定字符串内容的字符串缓冲区对象

StringBuffer的方法:

  public int capacity():返回当前容量。 理论值
  public int length():返回长度(字符数)。 实际值

StringBuffer的添加功能

public StringBuffer append(String str):   可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
 public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

StringBuffer的删除功能

ublic StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
 public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身,含头不含尾

StringBuffer的替换和反转功能

StringBuffer的替换功能
 public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
StringBuffer的反转功能
 public StringBuffer reverse():       字符串反转

StringBuffer的截取功能及注意事项

public String substring(int start):   从指定位置截取到末尾
 public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
 注意:返回值类型不再是StringBuffer本身

StringBuffer和String的相互转换

String -- StringBuffer
 a:通过构造方法
 b:通过append()方法
StringBuffer -- String
 a: 使用substring方法
 b:通过构造方法
 c:通过toString()方法

public class MyTest2 {
    public static void main(String[] args) {
        //String -----StringBuffer
        String str="abc";
        //方式1
        StringBuffer sb = new StringBuffer(str);
        //方式2
        StringBuffer sb2 = new StringBuffer().append(str);
        //StringBuffer----String
        StringBuffer sb3 = new StringBuffer("sbc");
        //方式1
        String string = sb3.toString();
        //方式2
        String substring = sb3.substring(0);
        //方式3
       /* String(StringBuffer buffer)
        分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列。*/
        String s = new String(sb3);
  }
}
把数组转成字符串
public class MyTest3 {
    public static void main(String[] args) {
      /*  A:
        案例演示
        需求:把数组中的数据按照指定个格式拼接成一个字符串
        举例:
        int[] arr = {1, 2, 3};
        输出结果:
        "[1, 2, 3]"
        用StringBuffer的功能实现*/
        int[] arr = {1, 2, 3};
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i < arr.length; i++) {
            if(i==arr.length-1){
                sb.append(arr[i]).append("]");
            }else{
                sb.append(arr[i]).append(",");
            }
        }
        String string = sb.toString();
        System.out.println(string);
    }
}

StringBuffer和StringBuilder的区别

基本上没啥区别,就是线程安全效率不一样

public class MyTest4 {
    public static void main(String[] args) {
        //主线程
        //StringBuffer和StringBuilder得区别
        //StringBuffer 线程安全得效率低
        //StringBuilder 线程安全效率高
        //里面得方法一摸一样
        StringBuffer stringBuffer = new StringBuffer();
        StringBuilder stringBuilder = new StringBuilder();
    }
}

数组常见操作

冒泡排序


public classMyTest {

public static void main(String[] args) {

//冒泡排序:数组元素 两两比较,大的往后放,经过一轮比较后,最大得元素会出现在最后面,如此往复,那么整个数组元素就有序了。

       
	int[] arr = {24, 69, 80, 57, 13, 20, -1, 0};
        //外层循环控制轮次       
	for (int j = 0; j < arr.length - 1; j++) {
           
 		for (int i = 0; i < arr.length - 1 - j; i++) {
              
			if (arr[i] > arr[i + 1]) {

                    	//交换位置

                    	int t = arr[i];
                    	arr[i] = arr[i + 1];
                    	arr[i + 1] = t;               
			}           
		}       
	}    
System.out.println(Arrays.toString(arr));
    }
选择排序


public class MyTest2{
    public static void main(String[] args) {
        //选择排序:每次拿一个元素和后面所有得元素挨个比较,小得往前放,经过一轮比交换,最小得元素会出现在最前面,如此往复,整个数组就排好序了。
        int[] arr = {24, 69, 80, 57,13,20,50,200,0,-1};
        for (int index = 0; index <
arr.length-1; index++) {
            for (int i = 1 + index; i <
arr.length; i++) {
                if (arr[index] > arr[i]) {
                    //交换位置
                    int t = arr[index];
                    arr[index] = arr[i];
                    arr[i] = t;
                }
            }
        }      
System.out.println(Arrays.toString(arr));
}
二分查找


public class MyTest2{
    public static void main(String[] args) {
        //查找思想:前提条件:数组元素必须有序,二分查找:每次查找一半
        int[] arr = {10, 20, 30, 50, 90, 100,101, 300, 400};
        int index = getIndex(arr, 90);
        System.out.println("索引是"+ index);
    }



//二分查找
    private static int getIndex(int[] arr, intele) {
        //定义三个索引
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        int centerIndex = (minIndex + maxIndex)/ 2;
        while (minIndex <= maxIndex) {
            if (ele == arr[centerIndex]) {
                return centerIndex;
            } else if (ele >arr[centerIndex]) {
                minIndex = centerIndex + 1;
            } else if (ele <
arr[centerIndex]) {
                maxIndex = centerIndex - 1;
           }
            //重新计算中间索引
            centerIndex = (minIndex + maxIndex)/ 2;
        }
        return -1;
    }
}

快速排序


 public class MyTest {
public static void main(String[] args) {
//快速排序
        int[] arr = {20, 40, 2, 3, 7, 8, 1, 0,9, 45, 30, 100, 200, 30, 50};
        QuickSortUtils.quickSort(arr, 0,arr.length - 1);       
System.out.println(Arrays.toString(arr));
    }
}

public class QuickSortUtils {
    public static void quickSort(int[] arr, intstart, int end) {
        //得对左右两区进行递归调用
        //为了分成左右两区那个索引位置
        if (start < end) {
            int index = getIndex(arr, start,end); //
            //对左区进行递归
            quickSort(arr, start, index - 1);
            //对右区进行递归
            quickSort(arr, index + 1, end);
        }
    }



//1.将基准数挖出形成第一个坑。
    //2.由后向前找比他小的数,找到后挖出此数填到前一个坑中。
    //3.由前向后找比他大或等于的数,找到后也挖出此数填到前一个坑中。
    //再重复执行2,3两步骤。
    private static int getIndex(int[] arr, intstart, int end) {
        int i = start;
        int j = end;
        //定义一个基准数
        int x = arr[i];
        while (i < j) {
            //由后向前找比他小的数,找到后挖出此数填到前一个坑中。
            while (i < j && arr[j]>= x) {//找
                j--;
            }
            //填坑
            if (i < j) {
                arr[i] = arr[j];
                i++;
            }


//3.由前向后找比他大或等于的数,找到后也挖出此数填到前一个坑中。
            while (i < j && arr[i]< x) {//找
                i++;
            }
            //填坑
            if (i < j) {
                arr[j] = arr[i];
                j--;                          
                 }
        }
        //将基准数填到最后一个坑中
        arr[i] = x;
 return i;
    }
}



Arrays类

针对数组进行操作的工具类。
提供了排序,查找等功能。

成员方法:

public static String toString(int[] a)
 public static void sort(int[] a)
 public static int binarySearch(int[] a,int key)
 static boolean equals(int[] a, int[] a2) 比较两个数组中的元素,是否一样
 static int[] copyOf(int[] original, int newLength)  复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
static int[] copyOfRange(int[] original, int from, int to) 从指定索引处,拷贝旧数组元素到你指定的终止索引处,复制到新的数组中,含头不含尾

Arrays类的源码解析

import java.util.Arrays;
public class MyTest {
    public static void main(String[] args) {
        //Java针对数组的操作,给我们提供了一个工具类Arrays
        //此类包含用来操作数组(比如排序和搜索)的各种方法。
        int[] arr = {10, 20, 5, 3, 8, 7, 6};
        //排序
        Arrays.sort(arr);
        //打印数组元素
        String string = Arrays.toString(arr);
        System.out.println(string);
        //二分查找:前提数组元素有序
        int index = Arrays.binarySearch(arr, 80);
        System.out.println(index);
    }

基本类型包装类

需求:
a:将100转换成二进制 , 八进制 , 十六进制
b:判断一个数是否在int的范围内

为什么会有基本类型包装类?
为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型.
基本数据类型不能调方法,基本数据包装类可以调方法,用起来方便

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

基本类型和包装类的对应
byte    Byte
short   Short
int   Integer
long   Long
float   Float
double          Double
char   Character
boolean           Boolean
public class MyTest {
    public static void main(String[] args) {
int num=100;
        String string = Integer.toBinaryString(num);
        String string1 = Integer.toHexString(num);
        String string2 = Integer.toOctalString(num);
        System.out.println(string);
        System.out.println(string2);
        System.out.println(string1);
        if(2000>=Integer.MIN_VALUE&& 2000<=Integer.MAX_VALUE){
            System.out.println("int 类型的值");
        }
    }
}
Integer类

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

构造方法

public Integer(int value)
public Integer(String s)

public class MyTest2 {
    public static void main(String[] args) {
        //Integer int的包装类  Integer 类在对象中包装了一个基本类型 int 的值。
        //构造方法
       /* Integer( int value)
        构造一个新分配的 Integer 对象,它表示指定的 int 值。
        Integer(String s)
        构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。*/
int num=100;
        Integer integer = new Integer(num);  //把基本类型,包装成他所对应的包装类
        //Integer integer1 = new Integer("abc"); //NumberFormatException 数字格式化异常
        Integer integer1 = new Integer("123"); //只能给字面上是数字的字符串
          }
}
String和int类型的相互转换
int -- String
 a:和""进行拼接
 b:public static String valueOf(int i)
 c:int -- Integer -- String
 d:public static String toString(int i)
 
 String -- int
 a:String -- Integer -- intValue();
 b:public static int parseInt(String s)

public class MyTest {
    public static void main(String[] args) {
        // String和int类型的相互转换
        //int------String
        int num = 100;//  ----->"100"
        //方式1
        String str = num + "";  //拼接空串
        //方式2 记忆这个
        //String 中 静态方法 valueOf(num) 可以把多种类型转换成字符串
        String s = String.valueOf(num);
      /*  char[]  chs={'a','b','c'};
        String s1 = new String(chs);
        //把字符数组转换成字符串
        String s2 = String.valueOf(chs);*/
        //方式3
        Integer integer = new Integer(num);
        String string = integer.toString();
        System.out.println(string);
        // String------int
        String strNum = "100";//----100
        //方式1
        Integer integer1 = new Integer(strNum);
        int i = integer1.intValue(); //把包装类型转成他对应的基本类型
        //方式2 记这个方法
        int nun = Integer.parseInt(strNum);
          }
}

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

JDK5的新特性:
自动装箱:把基本类型转换为包装类类型
自动拆箱:把包装类类型转换为基本类型

注意事项:
在使用时,Integer x = null;代码就会出现NullPointerException。
建议先判断是否为null,然后再使用。

  public class MyTest4 {
    public static void main(String[] args) {
        //超过1个字节的范围
        Integer i5 = 128; //自动装箱
        Integer i6 = 128;
        Integer.valueOf(128);
        System.out.println(i5 == i6); //false
//没有超过一个字节的范围
        Integer i7 = 127;
        Integer i8 = 127;
                System.out.println(i7 == i8); // true
        // Integer i7 = 127; 自动装箱  底层要调用
        //手动装箱
        Integer.valueOf(127);
         }
}


public class MyTest3 {
    public static void main(String[] args) {
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);//false
        //Integer类重写了equals方法比值是否相同
        System.out.println(i1.equals(i2));//true
        System.out.println("-----------");
	Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4);//false
        System.out.println(i3.equals(i4));//true
        System.out.println("-----------");
 	Integer i5 = 128; //自动装箱
        Integer i6 = 128;
                System.out.println(i5 == i6); //false
        System.out.println(i5.equals(i6)); //true
        System.out.println("-----------");
	Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8); //true
    }
}

public class MyTest2 {
    public static void main(String[] args) {
        //JDK1.5之后有的自动拆装箱
        //自动装箱:将基本类型自动转换成他所对应的包装类型
        //自动拆箱:将包装类型自动转换成他所对应的基本类型
        Integer integer = new Integer(20);
         Integer integer2 = new Integer(200);
       /* int a=20;
        int b=200;*/
        int num2=integer+integer2; //自动拆箱
        //收动拆箱
        int i = integer.intValue();
        int i1 = integer2.intValue();
        int num=i+i1;
        System.out.println(num);
        int aa=200;
        Integer integer1=aa;  //自动装箱
	   Integer integer3 = Integer.valueOf(200); //手动装箱
	   //都完成了哪些操作?
        Integer ii = 100; //自动装箱
        ii += 200;//自动拆箱,自动装箱
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值