Java从入门到放弃13---StringBuffer类/StringBuilder类/Arrays类/基本数据类型包装类/Integer类/Character类

Java从入门到放弃13—StringBuffer类/StringBuilder类/Arrays类/基本数据类型包装类/Integer类/Character类

01 Stringbuffer

  • StringBuffer类:可以理解为一个长度可变的字符容器或是一个字符串的缓冲区,储存的是线程安全的可变字符序列
  • StringBuffer和String的区别:字符串进行拼接操作时,每次拼接,都会构建一个新的String对象,即耗时,又浪费时间。且字符串一旦定义之后,长度是不可变的。而StringBuffer是一个长度可变的字符容器,可以存储多个字符,并且允许不断的往容器中增加内容,需要通过某些方法的调用改变容器中线程安全的可变字符序列的长度和内容。
  • StringBuffer类的构造方法
StringBuffer的无参构造方法
		public StringBuffer()//构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
StringBuffer的有参构造方法
		public StringBuffer(int capacity)//指定容量的字符串缓冲区对象
 		public StringBuffer(String str)//指定字符串内容的字符串缓冲区对象
  • StringBuffer类的常用方法
public int capacity()//返回当前容量(理论值)
public int length()//返回长度(字符数)(实际使用容量)
public StringBuffer append(String str)//可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public synchronized String toString()//StringBuffer类重写了toString方法,该方法可以将容器中的内容取出来转成字符
public StringBuffer insert(int offset,String str)//在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer deleteCharAt(int index)//删除指定位置的单个字符,并返回本身
public StringBuffer delete(int start,int end)//删除从指定位置开始指定位置结束的内容,并返回本身(含头不含尾)
public StringBuffer replace(int start,int end,String str)//从start开始到end用str替换
public StringBuffer reverse()//字符串反转 如把abcd转换为dcba
public int indexOf(String str)//返回第一次出现的指定子字符串在该字符串中的索引。           
public int indexOf(String str, int fromIndex)//从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。 
public int lastIndexOf(String str)//返回最右边出现的指定子字符串在此字符串中的索引。
public int lastIndexOf(String str, int fromIndex)//返回最后一次出现的指定子字符串在此字符串中的索引。
public String substring(int start)//从指定位置截取到末尾
public String substring(int start,int end)//截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
  • StringBuffer类对象<---------------------->String类对象
//String类转为StringBuffer类
//方式1:通过构造方法
String str="abc";
StringBuffer sb = new StringBuffer(str);
//方式2:通过append方法
StringBuffer sb2 = new StringBuffer().append(str);
StringBuffer sb3 = new StringBuffer().insert(0, str);
StringBuffer sb4 = sb.replace(0, str.length(), str);
--------------------------------------------------------
//StringBuffer类转为String类
//方式1:使用substring方法
StringBuffer strB=new StringBuffer("abc");
String str=strB.substring(0);
//方式2:通过构造方法,分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列。
String str=new String(strB);
//方式3:通过toString()方法
String str=strB.toString();
  • 案例1:把数组转换成字符串,并按规定形式输出
public class Test{
    public static void main(String[] args){
        int[] arr={1,2,3};
        StringBuffer strB=new StringBuffer("[");
        for(int i=0;i<arr.length;i++){
            if(i!=arr.length-1){
                strB.append(arr[i]).append(",");
            }else{
                strB.append(arr[i]).append("]");
            }
        }
        String str=strB.toString();
        System.out.println(str);
    }
}
//运行结果:[1,2,3]
  • StringBuffer和StringBuilder的区别

    答:StringBuilder是一个线程不安全的但效率高的字符串缓冲区,存储的也是一个可变的字符序列,此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。

  • String和StringBuffer分别作为参数传递的区别

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

    程序示例:

    public class Test{
        public static void main(String[] args){
            String str=new String("Hello World");
            setString(str);
            System.out.println(str);
            StringBuffer strB=new StringBuffer("str");
            setStringBuffer(strB);
            System.out.println(strB);
        }
        public static void setString(String str){
            str=str+"!";
            System.out.println(str);
        }
         public static void setStringBuffer(StringBuffer strB){
            strB.append(" Hello Java");
            System.out.println(strB);
        }
    }
    运行结果:Hello World!
    		Hello World
    		Hello World Hello Java
    		Hello World Hello Java
    

02 Arrays类

  • 常用Arrays类的方法
public static String toString(int[] a)//将数组遍历输出
public static int binarySearch(int[] a,int key)//使用二分法查找数组a中的key元素的索引,如果未查到则return -(low + 1);  
public static boolean equals ( int[] a, int[] a2)//如果两个指定的 int 型数组彼此相等,则返回 true。
public static void fill ( int[] a, int val)//将指定的 int 值分配给指定 int 型数组的每个元素。即用val将数组a覆盖
public static void sort ( int[] a, int fromIndex, int toIndex)//对指定 int 型数组a的指定范围按数字升序进行排序。
  • Arrays类的二分查找法(使用二分查找法,传入的数组必须有序)
public class BinarySearch{
    public static void main(String[] args){
        int[] arr={10,20,40,30,60,50,70,80,90};
        Arrays.sort(arr);
        binarySearch(arr,40);
    }
    private static int binarySearch(int[] arr,int key){
        int startIndex=0;
        int endIndex=arr.length-1;
        while(startIndex<=endIndex){
            int mid=startIndex+(endIndex-startInde)>>>1;//右移位运算,相当于除以2的1次方
            if(arr[mid]<key){
                startIndex=mid+1;
            }else if(arr[mid]>key){
                endIndex=mid-1;
            }else if(arr[mid]==key){
                return mid;
            }
        }
        return -(startIndex+1);//循环结束后,还没找到,返回-(startIndex+1)
    }
}

03 基本数据类型包装类

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

  • 基本类型和包装类的对应关系
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    char Character
    boolean Boolean

  • 应用:经常用于基本数据类型与字符串之间的转换。


04 Integer类

  • Integer 类在对象中包装了一个基本类型 int 的值,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
  • 构造方法
public Integer(int value)//构造一个新分配的 Integer 对象,它表示指定的 int 值。
public Integer(String s)//构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
  • 常用方法
public static String toString ()//返回一个表示该 Integer 值的 String 对象。
public static static String toString ( int i)//返回一个表示指定整数的 String 对象。
public static int intValue ()//以 int 类型返回该 Integer 的值。
public static int parseInt (String s)//将字符串参数作为有符号的十进制整数进行解析。
  • 案例1:将100转换成二进制 , 八进制 , 十六进制
public class MyDemo {
    public static void main(String[] args) {
        int num=100;
        String string = Integer.toBinaryString(num);
        String string1 = Integer.toOctalString(num);
        String string2 = Integer.toHexString(num);
        System.out.println(string);
        System.out.println(string1);
        System.out.println(string2);
    }
}
运行结果:1100100
		144
		64
  • 案例2:判断一个数是否在int的范围内
public class MyDemo{
    public static void main(String[] args){
        boolean b= 100000>=Integer.MIN_VALUE&& 100000<=Integer.MAX_VALUE?true:false;
        System.out.println(b);//MIN_VALUE和MAX_VALUE是Integer提供的一种属性,分别表示int数据类型的最小范围和最大范围
    }
}
//运行结果:true
  • JDK5的新特性:自动装箱和拆箱

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

Integer i1 = new Integer(127);//手动装箱
int i = integer.intValue();//手动拆箱
Integer i2 = 100;//自动装箱
i2 += 200;//自动拆箱后又自动装箱
  • String类型和int类型的相互转换
//String------->int
//方式1:String -->Integer --> intValue();
String str="1";
Integer i=new Integer(str);
int i=i.intvalve();
//方式2:public static int parseInt(String s)Integer类提供方法将字符串解析成int类型数据
int ii = Integer.parseInt(str2);
------------------------------------------------------------------------------------
//int------->String
//方式1:用+符号拼接
int num=1;
String str=num+"";
//方式2:public static String valueOf(int i)
String str=String.valveOf(num);
//方式3:int-->Integer-->String
String str=new Integer(num).toString();
//方式4:public static String toString(int i)
String str=Integer.toString(num);
  • Integer类型相关面试题
看程序写结果
	
	Integer i1 = new Integer(127);
	Integer i2 = new Integer(127);
	System.out.println(i1 == i2);
	System.out.println(i1.equals(i2));//Integer重写了父类的equals方法,比较的是值是否相等
	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自动装箱通过静态代码块直接创建了256个Integer的对象,从-128到127之间。当超过这个范围,会重新new一个Integer对象
	Integer i5 = 128;
	Integer i6 = 128;
	System.out.println(i5 == i6);//false  因为 超过了一个字节的范围 会new 一个Integer对象
	System.out.println(i5.equals(i6));
	System.out.println("-----------");

	Integer i7 = 127;
	Integer i8 = 127;
	System.out.println(i7 == i8);//true 没有超过一个字节的范围 因为在方法区中存在一个 字节常量池 范围-128---127
	System.out.println(i7.equals(i8));

05 Character类

  • 构造方法
public Character(char value)//构造一个新分配的 char 对象,它表示指定的 char 值。
public Character(String s)//构造一个新分配的 Character 对象,它表示 String 参数所指示的 char 值。
  • 常用方法
public static boolean isLetter ( char ch)//确定指定字符是否为字母。
public static boolean isLowerCase ( char ch)//确定指定字符是否为小写字母。
public static boolean isUpperCase ( char ch)//确定指定字符是否为大写字母。
public static boolean isDigit ( char ch)//确定指定字符是否为数字。
public static boolean isSpaceChar ( char ch)//确定指定字符是否为 Unicode 空白字符。
  • 示例
public class MyTest6 {
    public static void main(String[] args) {
        char ch='a';
        Character character = new Character(ch);//手动装箱
        char c = character.charValue();//手动拆箱
        boolean spaceChar = Character.isSpaceChar(' ');
        System.out.println(spaceChar);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值