常见对象
StringBuffer
StringBuffer类的概述
StringBuffer类概述
- 我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时又浪费空间。而StringBuffer就可以解决这个问题,线程安全的可变字符序列
public class MyTest {
public static void main(String[] args) {
/* StringBuffer()
构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。*/
//创建一个空的字符容器。
StringBuffer s = new StringBuffer();
//构建时,可以指定这个字符容器的容量
StringBuffer sb = new StringBuffer(100);
//构建容器时,可以往容器中放入字符串
StringBuffer sb2= new StringBuffer("abbcdfdfd");
//往容器中追加数据
s.append("abcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//获取容器的容量 一旦超过容量,可以自动扩容。
int capacity = s.capacity();
System.out.println(capacity);//57
System.out.println(sb.capacity());//100
//获取容器的长度。
int length = s.length();
System.out.println(length);//57
//容量 ,长度
}
}
public class MyTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
//append("abc"); 往容器中追加数据,返回的还是原来的那个容器对象。
StringBuffer sb2 = sb.append("abc");
System.out.println(sb==sb2);//true
sb.append(100).append(200).append(true).append(new char[]{'a','b','c'});
//StringBuffer 重写了toString()方法,然后把容器中的数据转换成字符串返回
System.out.println(sb.toString());//"abc100200trueabc"
}
}
StringBuffer类的构造方法
- public StringBuffer():无参构造方法
public class MyTest {
public static void main(String[] args) {
//创建一个无参构造
StringBuffer s = new StringBuffer();
s.append(2).append('a').append(new char[]{'c','a','t'});
System.out.println(s.toString());//2acat
}
}
- public StringBuffer(int capacity):指定容量的字符串缓冲区对象
public class MyTest {
public static void main(String[] args) {
//创建一个指定容量的字符串缓冲区对象
StringBuffer s = new StringBuffer(22);
//容器长度
System.out.println(s.length());//0
//容器容量
System.out.println(s.capacity());//22
}
}
- public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
public class MyTest {
public static void main(String[] args) {
//创建一个指定字符串内容的字符串缓冲区对象
StringBuffer s = new StringBuffer("a");
System.out.println(s.length());//1
System.out.println(s.capacity());//17
System.out.println(s.toString());//a
}
}
StringBuffer的方法
- public int capacity():返回当前容量。理论值
- public int length():返回长度(字符数)。实际值
public class MyTest {
public static void main(String[] args) {
StringBuffer s= new StringBuffer();
s.append(123);
//返回当前容量,理论值
System.out.println(s.capacity());
//返回长度(字符数),实际值
System.out.println(s.length());
}
}
StringBuffer的删除功能
StringBuffer的删除功能
- public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
public class MyTest {
public static void main(String[] args) {
//删除指定位置的字符,并返回本身
StringBuffer s= new StringBuffer("1234567");
s.deleteCharAt(2);
System.out.println(s);//124567
}
}
- public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
public class MyTest {
public static void main(String[] args) {
//删除从指定位置开始指定位置结束的内容,并返回本身
//含头不含尾
StringBuffer s= new StringBuffer("1234567");
s.delete(2, 4);
System.out.println(s);//12567
}
}
StringBuffer的替换、插入、反转和查找功能
替换功能
- public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
public class MyTest {
public static void main(String[] args) {
//从start开始到end用str替换
StringBuffer s= new StringBuffer("1234567");
s.replace(2, 4, "abc");
System.out.println(s);//12abc567
}
}
插入功能
- public StringBuffer insert(int offset,String str):从指定位置插入内容
public class MyTest {
public static void main(String[] args) {
StringBuffer s= new StringBuffer("1234567");
s.insert(2, "aa");
System.out.println(s);//12aa34567
}
}
反转功能
- public StringBuffer reserve():反转,返回容器本身
public class MyTest {
public static void main(String[] args) {
//反转,返回容器本身
StringBuffer s= new StringBuffer("1234567");
s.reverse();
System.out.println(s);//7654321
}
}
查找功能
- public int indexOf (String str):从头查找该字符串,在容器中第一次出现的索引,如果找不到就返回-1
public class MyTest {
public static void main(String[] args) {
//从头查找该字符串,在容器中第一次出现的索引,如果找不到就返回-1
StringBuffer s= new StringBuffer("1234567");
System.out.println(s.indexOf("3"));//2
System.out.println(s.indexOf("9"));//-1
}
}
- public int indexOf (String str,int fromIndex):从指定索引处开始查找该字符串第一次出现的索引,如果找不到就返回-1
public class MyTest {
public static void main(String[] args) {
//从指定索引处开始查找该字符串第一次出现的索引,如果找不到就返回-1
StringBuffer s= new StringBuffer("12345671234567");
System.out.println(s.indexOf("1", 1));//7
System.out.println(s.indexOf("1", 8));//-1
}
}
- public int lastIndexOf (String str):从后往前查找该字符串,在容器中第一次出现的索引,如果找不到就返回-1
public class MyTest {
public static void main(String[] args) {
//从后往前查找该字符串,在容器中第一次出现的索引,如果找不到就返回-1
StringBuffer s= new StringBuffer("12345671234567");
System.out.println(s.lastIndexOf("1"));//7
System.out.println(s.lastIndexOf("8"));//-1
}
}
- public int lastIndexOf (String str,int fromIndex):从指定索引处从后往前开始查找该字符串第一次出现的索引,如果找不到就返回-1
public class MyTest {
public static void main(String[] args) {
//从指定索引处从后往前开始查找该字符串第一次出现的索引,如果找不到就返回-1
StringBuffer s= new StringBuffer("12345671234567");
System.out.println(s.lastIndexOf("1", 5));//0
System.out.println(s.lastIndexOf("7", 5));//-1
}
}
StringBuffer的截取功能及注意事项
StringBuffer的截取功能
- public String substring(int start):从指定位置截取到末尾
public class MyTest {
public static void main(String[] args) {
//从指定位置截取到末尾
StringBuffer s= new StringBuffer("12345671234567");
System.out.println(s.substring(5));//671234567
}
}
//返回的是String类型
- public String substring(int start,int end):截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
public class MyTest {
public static void main(String[] args) {
//截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
//含头不含尾
StringBuffer s= new StringBuffer("12345671234567");
System.out.println(s.substring(2, 5));//345
}
}
//返回的是String类型
StringBuffer和String的相互转换
String – StringBuffer
public class MyTest {
public static void main(String[] args) {
//String -- StringBuffer
String str="abc";
//方式1
StringBuffer sb = new StringBuffer(str);
//方式2:
StringBuffer sb2 = new StringBuffer();
sb2.append(str);
//方式3:
StringBuffer sb3 = new StringBuffer();
sb3.insert(0,str);
}
}
StringBuffer – String
public class MyTest {
public static void main(String[] args) {
//StringBuffer -- String
StringBuffer sb = new StringBuffer("aaaaa");
//方式1:toString()
String s = sb.toString();
String s1 = sb.substring(0);
//String 类中有一个构造方法
String s2 = new String(sb);
}
}
把数组转成字符串
public class MyTest {
public static void main(String[] args) {
//把数组中的数据按照指定个格式拼接成一个字符串
int[] arr={1,2,3,4,5,6,7};
StringBuffer s = new StringBuffer("[");
for (int i = 0; i < arr.length; i++) {
if(i==arr.length-1){
s.append(arr[i]+"]");
}else{
s.append(arr[i]+",");
}
}
System.out.println(s.toString());//[1,2,3,4,5,6,7]
}
}
字符串反转
import java.util.Scanner;
public class MyTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入");
String i = sc.nextLine();//abc
//把输入内容放入字符串缓冲区
StringBuffer s = new StringBuffer(i);
System.out.println(s.reverse());//cba
}
}
StringBuffer和StringBuilder的区别
- StringBuffer 线程安全的字符序列,效率低
- StringBuilder 一个可变的字符序列,线程不安全的,效率高
- 此类提供一个与 StringBuffer 兼容的 API,但不保证同步
- 该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)
- 如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快
String和StringBuffer分别作为参数传递
形式参数问题
- String作为参数传递 String虽然是引用类型,但是它是一个常量,所以在做传递的时候,完全可以将其看成基本数据类型数据进行传递,值传递
- StringBuffer作为参数传递,作为引用类型传递
public class MyTest3 {
public static void main(String[] args) {
//作为值传递
String m="你好";
change(m);
//作为引用类型传递
StringBuffer s = new StringBuffer("你真棒");
change(s);
System.out.println(m);//你好
System.out.println(s);//你真棒鹿秀儿
}
public static void change(StringBuffer s) {
s.append("鹿秀儿");
}
public static void change(String m){
m+="儿秀鹿";
System.out.println(m);//你好儿秀鹿
}
}
Arrays类
Arrays类的概述和方法使用
Arrays类概述
- 针对数组进行操作的工具类
- 提供了排序,查找等功能
成员方法
- public static String toString(int[ ] a):把数组以字符串形式展示
import java.util.Arrays;
public class MyTest {
public static void main(String[] args) {
//把数组以字符串形式展示
int[] arr = new int[]{1,2,3,4,5,6,7,8};
System.out.println(Arrays.toString(arr));//[1, 2, 3, 4, 5, 6, 7, 8]
}
}
- public static void sort(int[ ] a):对数组进行排序,并将排序结果返回到原数组中
import java.util.Arrays;
public class MyTest {
public static void main(String[] args) {
//对数组进行排序,并将排序结果返回到原数组中
int[] arr = new int[]{2,6,4,7,9,3,1};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));//[1, 2, 3, 4, 6, 7, 9]
}
}
- public static int binarySearch(int[ ] a,int key):使用二分搜索算法在指定的数组中搜索指定的值,并返回该值所在索引位置;若查询不到,则返回 -(起始索引+1)
import java.util.Arrays;
public class MyTest {
public static void main(String[] args) {
//使用二分搜索算法在指定的数组中搜索指定的值,并返回该值所在索引位置;若查询不到,则返回-1
int[] arr = new int[]{2,6,4,7,9,3,1};
//这个数组要有序
Arrays.sort(arr);//[1, 2, 3, 4, 6, 7, 9]
System.out.println(Arrays.binarySearch(arr, 7));//5
System.out.println(Arrays.binarySearch(arr, 5));//-5
}
}
- static boolean equals(int[ ] a,,int[ ] a2):比较两个数组中的元素,是否一样
import java.util.Arrays;
public class MyTest {
public static void main(String[] args) {
//比较两个数组中的元素,是否一样
int[] arr1 = new int[]{2,4,6,8};
int[] arr2 = new int[]{2,4,6,8};
int[] arr3 = new int[]{4,6,8,2};
System.out.println(Arrays.equals(arr1, arr2));//true
System.out.println(Arrays.equals(arr1, arr3));//false
}
}
- static int[ ] copyOf(int[ ] original,int newLength): 复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
import java.util.Arrays;
public class MyTest {
public static void main(String[] args) {
//复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
int[] arr = new int[]{2,4,6,8};
int[] arrc = Arrays.copyOf(arr, 3);
System.out.println(Arrays.toString(arrc));//[2, 4, 6]
}
}
- static int[ ] copyOfRange(int[ ] original,int from,int to):复制旧数组中的指定范围间的几个元素到新数组中
import java.util.Arrays;
public class MyTest3 {
public static void main(String[] args) {
//复制旧数组中的指定范围间的几个元素到新数组中
//含头不含尾
int[] arr = new int[]{2,4,6,8};
int[] arrc = Arrays.copyOfRange(arr, 0, 2);
System.out.println(Arrays.toString(arrc));//[2, 4]
}
}
基本类型包装类的概述
- 将100转换成二进制
public class MyTest {
public static void main(String[] args) {
//将100转换成二进制
int num=100;
String s=Integer.toBinaryString(num);
System.out.println(s);//1100100
}
}
- 将100转换成八进制
public class MyTest {
public static void main(String[] args) {
//将100转换成八进制
int num=100;
String s = Integer.toOctalString(num);
System.out.println(s);//144
}
}
- 将100转换成十六进制
public class MyTest {
public static void main(String[] args) {
将100转换成十六进制
int num=100;
String s = Integer.toHexString(num);
System.out.println(s);//64
}
}
- 判断一个数是否在int范围内
public class MyTest {
public static void main(String[] args) {
//判断一个数是否在int范围内
int num=3333333;
if(num<=Integer.MAX_VALUE&&num>=Integer.MIN_VALUE){
System.out.println("在int范围内");
}else{
System.out.println("不在int范围内");
}
}
}
为什么会有基本类型包装类
- 为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型
- 常用操作:用于基本数据类型与字符串之间的转换
基本类型和包装类的对应
基本类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer类的概述和构造方法
Integer类概述
- Integer 类在对象中包装了一个基本类型 int 的值
- 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换
- 还提供了处理 int 类型时非常有用的其他一些常量和方法
构造方法
- public Integer(int value):构造一个新分配的 Integer 对象,它表示指定的 int 值
public class MyTest {
public static void main(String[] args) {
//把int类型的值,包装成引用类型
int num=100;
Integer integer = new Integer(num);
}
}
- public Integer(String s):构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值
ublic class MyTest {
public static void main(String[] args) {
//要的这个字符串,字面上是一个数字的字符串
Integer integer = new Integer("2000");
}
}
String和int类型的相互转换
- int----String
public class MyTest {
public static void main(String[] args) {
//int----String
int num=100; //"100"
//方式1:和""进行拼接
String str=num+"";
//方式2:public static String valueOf(int i)
String s = String.valueOf(num);
//方式3:int -- Integer -- String
//public static String toString(int i)
Integer integer = new Integer(num);
String s1 = integer.toString();
}
}
- String----int
public class MyTest {
public static void main(String[] args) {
String strNum="100";
//方式1:String -- Integer -- intValue()
Integer integer1 = new Integer(strNum);
int i = integer1.intValue();
//方式2:public static int parseInt(String s)
int i1 = Integer.parseInt(strNum);
}
}
JDK5的新特性自动装箱和拆箱
JDK5的新特性
- 自动装箱:把基本类型转换为包装类类型
- 自动拆箱:把包装类类型转换为基本类型
public class MyTest {
public static void main(String[] args) {
Integer a = new Integer(100);
Integer b = new Integer(100);
//自动拆箱
int r=a+b;
System.out.println(r);
//手动拆箱 intValue();
Integer c = new Integer(100);
Integer d = new Integer(100);
//手动拆箱
int i = c.intValue();
int i1 = d.intValue();
System.out.println(i+i1);
System.out.println("====================================");
int num=10;
//自动装箱
//其实在调用ValueOf()方法 ValueOf()中使用到了IntegerCache这个Integer的静态内部类
Integer nmuInteger=num;
//手动装箱 Integer.valueOf(num);
Integer integer = Integer.valueOf(num);
}
}
看程序写结果
public class MyTest {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);//false
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
System.out.println(i7.equals(i8));//true
}
}
//Integer 重写了.equals()方法,比较的是他包装的这个值是否相同
//Integer 重写了toString()方法,把他包装的这个int类型的数据,转换成字符串
完