stringbuffer
可以解决拼串操作,也叫做线程安全的可变序列。
stringbuffer的构造方法
StringBuffer的构造方法:
public StringBuffer(): 无参构造方法
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值
长度:长度不能超过容量,创建的时候可以自己指定容器的容量。
append:往容器中追加容量。返回的还是原来的容器对象
public StringBuffer append(String str):
可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
insert:插入功能,往容器中的指定位置插入数据,返回的还是原来的容器。
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(): 字符串反转
indexof在这里依旧能用,indexof(内容)
int indexof(string str)
从头查找该字符串第一次在容器中出现的索引,如果找不到就返回-1;
int indexof(string str,int fromindex)
从指定索引处开始查找该字符串第一次出现的索引,如果找不到就返回-1;
从后往前找
int lastindexof(string str)
int lastindexof(string str,int fromindex)
substring返回的是一个字符串,然后截取的内容是含头不含尾。
stringbuffer的截取功能
public string substring(int start):从指定位置截取到末尾
public string substring(int start,int end):截取从指定位置开始到结束位置,包含开始位置,不包含结束位置。
返回值类型不再是stringbuffer本身。
stringbuffer和string的相互转换
append()方法
substring()方法
.tostring()可以用来将buffer转换为字符串。
把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
输出结果:
“[1, 2, 3]”
用StringBuffer的功能实现
package cn.itcast;
public class test8 {
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 s = sb.toString();
System.out.println(s);
}
}
把字符串反转
举例:键盘录入"abc"
输出结果:“cba”
用StringBuffer的功能实现
package cn.itcast;
import java.util.Scanner;
public class test9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入字符串");
String s = sc.nextLine();
String s1 = new StringBuffer(s).reverse().toString();
System.out.println(s1);
}
}
stringbuffer和stringbuilder的区别
stringbuffer 线程安全的字符序列,效率低
stringbuilder 一个可变的字符序列,线程不安全,效率高,这个提供了一个与stringbuffer兼容的API,但不保证同步。
该类设计用作stringbuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候,这个线程很普遍
该类比stringbuffer要快,通常采用该类
string和stringbuffer分别作为参数传递
string作为值传递,虽然是引用数据类型,但是它是一个常量,也就是字符串一旦被改变,值不能被改变。
其他类型的引用类型作为参数传递,形参的改变会影响实参。
stringbuffer作为参数传递
public static void main(String[] args) {
//引用类型作为参数传递,形参的改变会影响实参。
//注意:String 类型作为参数传递,属于值传递。会这把字符串的值传递过去
String str = "abc";
change(str);
System.out.println(str);//abc abc你好
StringBuffer sb = new StringBuffer("Hello");
change(sb);
sb.reverse();
System.out.println(sb); //呵呵哈哈olleH
}
private static void change(StringBuffer sb) {
sb.append("哈哈").append("呵呵");
}
private static void change(String str) {
str+="你好";
System.out.println(str); //abc你好
}
}
package cn.itcast;
class Demo4 {
public static void main(String[] args) {
StringBuffer buffer1 = new StringBuffer("abc");
StringBuffer buffer2 = new StringBuffer("abc");
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);//false
System.out.println(s1 = s2);//abc
System.out.println(buffer1 == buffer2);//false,引用参数指向的是地址值
System.out.println(buffer1.equals(buffer2));//false,转换成字符值就相等了
}
}
数组的排序
把数组中无序的元素,通过交换,移动等方式,使数组中的元素,成为一个有序序列。
排序的手段:
冒泡排序;
数组中的元素两两比较,大的元素往后调整,经过一轮比较后,最大的元素会出现在最后面,
m个元素比m-1次
每n轮里面比较m-1-n次。
package com.itheima.demo7;
import java.util.Arrays;
public class test6 {
public static void main(String[] args) {
int[] arr = {24, 2, 1, 4, 76, 32, 85};
for (int i = 0; i < arr.length - 1; i++) {//轮次
for (int j = 0; j < arr.length - 1 - i; j++) {//每一轮里面的比较次数。
if (arr[i] > arr[i + 1]) {
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
System.out.println(Arrays.toString(arr));//以数组形式的字符串打印出来
}
}
选择排序
每次拿一个元素跟后面剩余的元素比较,经过一轮比较后,小的会出现在最前面。
数组中5个元素经过4轮比较,第一轮从0索引处开始,比较了4次,
第二轮从1索引处开始,比较了3次
第三轮从2索引处开始,比较了2次
第4轮从3索引处开始,比较了1次。
package com.itheima.demo7;
import java.util.Arrays;
public class test7 {
public static void main(String[] args) {
int[] arr = {24, 2, 1, 4, 76, 32, 85};
for (int index = 0; index < arr.length; index++) {//外层循环,从0索引处开始
for (int i = index + 1; i < arr.length; i++) {//从index+1处开始比较,一直比较到最后一个索引处的元素的值
if (arr[index] > arr[i]) {
int t = arr[index];
arr[index] = arr[i];
arr[i] = t;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
直接插入排序
从第二个元素开始,每次拿一个元素插入之前的序列中,使之扔保持有序。
package com.itheima.demo7;
import java.util.Arrays;
public class test8 {
public static void main(String[] args) {
int[] arr = {24, 2, 1, 4, 76, 32, 85};
for(int i=1;i<arr.length;i++){//循环轮次
for(int j=i;j>0;j--){
if(arr[j]<arr[j-1]){
int t = arr[j];
arr[j]=arr[j-1];
arr[j-1]=t;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
快速排序
找到一个基准点,然后设置该数列的最左边的元素为左标记,向右移动直到找到比基准点大的就不再移动,将该数列的最右边的元素标记为右标记,向左移动,直到找到一个比基准点小的元素就不再移动,然后交换左右的标记所指的值,直到左右标记相互重合后将该点与基准点进行交换,然后无限次这样轮回,将所有小的元素都放在基准点的左边,将所有大的放在基准点的右边。然后产生了左区和右区,分别对左区和右区进行以上重复的操作。
通常都将最右边的元素作为基准元素。
二分查找
前提:数组元素是有序的
package com.itheima.demo7;
public class test9 {
public static void main(String[] args) {
int[] arr = {24, 2, 1, 4, 76, 32, 85};
int index = getIndex(arr, 32);//创造方法,获取元素
System.out.println(index);
}
private static int getIndex(int[] arr, int i) {
int minindex = 0;
int maxindex = arr.length - 1;
while (minindex <= maxindex) {
int centerindex = (minindex + maxindex) / 2;
if (i == arr[centerindex]) {
return centerindex;
} else if (i < arr[centerindex]) {
maxindex = centerindex - 1;
} else {
minindex = centerindex + 1;
}
}
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) 复制旧数组中的指定范围间的几个元素到新数组中
package cn.itcast;
import java.util.Arrays;
public class test10 {
public static void main(String[] args) {
int[] arr = {22,33,4,5,1,76,9,143,456,8};
String s = Arrays.toString(arr);
System.out.println(s);//将数组转换成字符串。
Arrays.sort(arr);//快速排序
int i = Arrays.binarySearch(arr, 8);//二分查找
System.out.println(i);
}
}
package cn.itcast;
import java.util.Arrays;
public class test11 {
public static void main(String[] args) {
int[] arr = {5,443,776};
int[] arr2 = {8,24,87,44};
boolean eq = Arrays.equals(arr, arr2);
System.out.println(eq);
}
}
package cn.itcast;
import java.lang.reflect.Array;
import java.util.Arrays;
public class test12 {
public static void main(String[] args) {
int[] arr = {12,43,6,86,5,0,23,3};
int[] ints = Arrays.copyOf(arr, 3);
String s = Arrays.toString(ints);
System.out.println(s);
int[] ints1 = Arrays.copyOfRange(arr, 2, 4);
System.out.println(Arrays.toString(ints1));
}
}