A:StringBuffer的构造方法:
public StringBuffer(): 无参构造方法
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
B:StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值
C:案例演示
构造方法和长度方法的使用
public class MyTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
int capacity = sb.capacity();
System.out.println(capacity);
StringBuffer sb2 = sb.append("asdfg");
System.out.println(sb==sb2);
sb.append("13").append("ahsuisah").append("oihgfc");
String string=sb.toString();
System.out.println(string);
int length=sb.length();
System.out.println(length);
int capacity1 = sb.capacity();
System.out.println(capacity1);
}
}
常见对象(StringBuffer的添加功能)
A:StringBuffer的添加功能
public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
B:案例演示
StringBuffer的添加功能
public class MyTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("aaa").append("kkk");
StringBuffer sb2 = sb.insert(2, "ddd");
System.out.println(sb2==sb);
System.out.println(sb);
}
}
常见对象(StringBuffer的删除功能)
A:StringBuffer的删除功能
public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
B:案例演示
StringBuffer的删除功能
public class MyTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("aaa").append("kkk");
StringBuffer sb2 = sb.insert(2, "ddd");
System.out.println(sb2==sb);
System.out.println(sb);
sb.deleteCharAt(0);
System.out.println(sb);
sb.delete(2,4);
System.out.println(sb);
}
}
常见对象(StringBuffer的替换和反转功能)
A:StringBuffer的替换功能
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
B:StringBuffer的反转功能
public StringBuffer reverse(): 字符串反转
C:案例演示
StringBuffer的替换和反转功能
public class MyTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("好好学习");
sb.replace(0,3,"天天向上");
System.out.println(sb);
sb.reverse();
System.out.println(sb);
}
}
常见对象(StringBuffer的截取功能及注意事项)
A:StringBuffer的截取功能
public String substring(int start): 从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
B:注意事项
注意:返回值类型不再是StringBuffer本身
C:案例演示
StringBuffer的截取功能及注意事项
public class MyTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("好好学习,天天向上");
String substring = sb.substring(0, 4);
System.out.println(substring);
System.out.println(sb);
}
}
常见对象(StringBuffer和String的相互转换)
A:String -- StringBuffer
a:通过构造方法
b:通过append()方法
B:StringBuffer -- String
a: 使用substring方法
b:通过构造方法
c:通过toString()方法
C:案例演示
StringBuffer和String的相互转换
public class MyTest {
public static void main(String[] args) {
String str="asdfg";
StringBuffer sb = new StringBuffer(str);
StringBuffer sb2 = new StringBuffer().append(str);
StringBuffer sb3=new StringBuffer("zxcv");
String string=sb3.toString();
String s=sb3.substring(0);
String s1=new String(sb3);
}
}
常见对象(把数组转成字符串)
A:案例演示
需求:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
输出结果:
"[1, 2, 3]"
用StringBuffer的功能实现
public class MyTest6 {
public static void main(String[] args) {
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);
}
}
常见对象(字符串反转)
A:案例演示
需求:把字符串反转
举例:键盘录入"abc"
输出结果:"cba"
用StringBuffer的功能实现
public class MyTest7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一段字符串");
String s = sc.nextLine();
StringBuffer stringBuffer = new StringBuffer(s);
String string = stringBuffer.reverse().toString();
System.out.println(string);
}
}
A:冒泡排序原理
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
B:案例演示
数组高级冒泡排序代码
public class MyTest8 {
public static void main(String[] args) {
int[] arr = {24, 69, 80, 57, 13};
for (int j = 0; j < 4; j++) {
for (int i = 0; i < arr.length - 1; 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));
}
}
常见对象(数组高级选择排序原理)
A:选择排序原理
从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
B:案例演示
public class MyTest {
public static void main(String[] args) {
int[] arr={24,69,80,57,13};
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));
}
}
常见对象(数组直接插入排序原理)
A:直接插入排序:每次把后面的一个元素,插入到之前的一个有序序列当中,使之仍保持有序。
B:案例演示
public class MyTest {
public static void main(String[] args) {
int[] arr={20,3,0,8,1,70,100,50};
for (int i = 1; i < arr.length ; i++) {
int j=i;
while (j>0&&arr[j]<arr[j-1]){
int t=arr[j];
arr[j]=arr[j-1];
arr[j-1]=t;
j--;
}
}
System.out.println(Arrays.toString(arr));
}
}
常见对象(数组快速排序原理图解)
A:快速排序
分治法:比大小,再分区
1.从数组中取出一个数,作为基准数。
2.分区:将比这个数大或等于的数全放到他的右边,小于他的数全放到他的左边。
3.再对左右区间重复第二步,直到各区间只有一个数。
B:案例演示
public class MyTest {
public static void main(String[] args) {
int[] arr={50,4,3,8,9,10,25,14};
QuickSortUtils.quickSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
public class QuickSortUtils {
public static void quickSort(int[] arr, int statr, int end) {
if (statr < end) {
int index = getIndex(arr, statr, end);
quickSort(arr, statr, index - 1);
quickSort(arr, index + 1, end);
}
}
private static int getIndex(int[] arr, int statr, int end) {
int i = statr;
int j = end;
int x = arr[i];
while (i < j && arr[j] >= x) {
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < x) {
i++;
}
if (i < j) {
arr[j] = arr[j];
j--;
}
arr[i] = x;
return i;
}
}
二分查找
A:二分查找:前提数组元素必须有序
B:案例演示
public class MyTest {
public static void main(String[] args) {
int[] arr={100,200,300,400,500,600,700,800,900};
int index=getIndex(arr,900);
System.out.println(index);
}
private static int getIndex(int[] arr, int num) {
int minIndex=0;
int maxIndex=arr.length-1;
int cenIndex=(minIndex+maxIndex)/2;
while (minIndex<=maxIndex){
if (num==arr[cenIndex]){
return cenIndex;
}else if(num<arr[cenIndex]){
maxIndex=cenIndex-1;
}else if(num>arr[cenIndex]){
minIndex=cenIndex+1;
}
cenIndex=(minIndex+maxIndex)/2;
}
return -1;
}
}
常见对象(Arrays类的概述和方法使用)
A:Arrays类概述
针对数组进行操作的工具类。
提供了排序,查找等功能。
B:成员方法
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) 复制旧数组中的指定范围间的几个元素到新数组中
C:案例演示
通过Arrays类的功能来进排序和查找
public class MyTest {
public static void main(String[] args) {
int[] arr={1,2,3,4};
System.out.println(Arrays.toString(arr));
int[] arr2={1,2,3,4,20,55,48,88,62,33,29};
Arrays.sort(arr2);
Arrays.sort(arr2,0,arr2.length-1);
System.out.println(Arrays.toString(arr2));
int[] arr3={1,2,3,4,88,15,23,34,48,27,61,14,10};
int i=Arrays.binarySearch(arr3,27);
System.out.println(i);
int[] arr4={1,2,3,4};
int[] arr5={1,2,3,4};
boolean equals=Arrays.equals(arr4,arr5);
System.out.println(equals);
int[] arr6={1,2,3,4,80,12,99,62,67,73,31};
int[] ints = Arrays.copyOf(arr6, 4);
System.out.println(Arrays.toString(ints));
int[] newArr = Arrays.copyOfRange(arr6, 4, 7);
System.out.println(Arrays.toString(newArr));
}
}
常见对象(基本类型包装类的概述)
A: 需求:
a:将100转换成二进制 , 八进制 , 十六进制
b:判断一个数是否在int的范围内
B:为什么会有基本类型包装类
为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型.
C:常用操作: 常用的操作之一:用于基本数据类型与字符串之间的转换。
D:基本类型和包装类的对应
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
常见对象(Integer类的概述和构造方法)
A:Integer类概述
通过JDK提供的API,查看Integer类的说明
Integer 类在对象中包装了一个基本类型 int 的值,
该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
还提供了处理 int 类型时非常有用的其他一些常量和方法
B:构造方法
public Integer(int value)
public Integer(String s) //要个一个字面上是数字的字符串,如果不是就会报错
常见对象(String和int类型的相互转换)
A:int -- String
a:和""进行拼接
b:public static String valueOf(int i)
c:int -- Integer -- String
d:public static String toString(int i)
B:String -- int
a:String -- Integer -- intValue();
b:public static int parseInt(String s)
C:案例演示
String和int类型的相互转换
public class MyTest {
public static void main(String[] args) {
int num=100;
Integer integer = new Integer(num);
String string = integer.toString();
System.out.println(string);
String strNum="1000";
Integer integer1 = new Integer(strNum);
int i = integer1.intValue();
System.out.println(i);
int num2 = Integer.parseInt(strNum);
System.out.println(num2);
}
}
常见对象(JDK5的新特性自动装箱和拆箱)
A:JDK5的新特性
自动装箱:自动将一个基本类型转换成所对应的包装类型;
自动拆箱:自动将一个包装类型转换成所对应的基本类型;
B:注意事项
在使用时,Integer x = null;代码就会出现NullPointerException。
建议先判断是否为null,然后再使用。