day13目录:
StringBuffer
StringBuilder
数组常见操作
Arrays
基本数据类型包装类
13.01_常见对象(StringBuffer类的概述)(理解)
A:StringBuffer类概述
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。
而StringBuffer就可以解决这个问题
线程安全的可变字符序列
B:简述安全问题
C:StringBuffer和String的区别:StringBuffer 可变字符序列,他就相当于一个字符容器,可以不断的往容器中追加字符
public class MyTest {
public static void main(String[] args) {
// StringBuffer 可变字符序列,他就相当于一个字符容器,可以不断的往容器中追加字符。
String s1="abc";
String s2="abaa";
String s3=s1+s2;
/* StringBuffer()
构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。*/
StringBuffer sb = new StringBuffer();
int capacity = sb.capacity();
System.out.println(capacity);//16
//可以指定容量
StringBuffer buffer = new StringBuffer(100);
System.out.println(buffer.capacity());//100
//往容器中追加内容
sb.append("abc");
int length = sb.length();
System.out.println(length);//3
}
}
13.02_常见对象(StringBuffer类的构造方法)(掌握)
A:StringBuffer的构造方法:
public StringBuffer(): 无参构造方法
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
B:StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值
C:案例演示
构造方法和长度方法的使用
public class MyTest2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());//16
//往容器中追加内容,一旦超过容量,会自动扩容
sb.append("abc阿斯顿发送到发斯蒂芬阿斯顿发斯蒂芬爱的发声的范阿斯顿发送到阿森松岛发上述的发到阿斯顿发送到");
System.out.println(sb.length());//48
System.out.println(sb.capacity());//48
//容量capacity() 长度length()
//水杯(500ML capacity()) 实际你接了200ML的水 长度length()
String s = sb.toString();
System.out.println(s);//abc阿斯顿发送到发斯蒂芬阿斯顿发斯蒂芬爱的发声的范阿斯顿发送到阿森松岛发上述的发到阿斯顿发送到
System.out.println(s.length());//48
}
}
13.03_常见对象(StringBuffer的添加功能)(掌握)
A:StringBuffer的添加功能
public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
B:案例演示
StringBuffer的添加功能:append只能加到后面,insert加到特定位置
public class MyTest3 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
//返回的还是容器本身
StringBuffer sb2 = sb.append(100);
sb.append("2222").append(true).append("asdfasdf").append(3.25);
System.out.println(sb2==sb);//true;所以上面两句可以不用变量收
// StringBuffer 重写了toString()方法,把存在容器中的数据,转换成字符串返回
String s = sb.toString();
System.out.println(s);//1002222trueasdfasdf3.25
StringBuffer ssbb = new StringBuffer("abc");
ssbb.append("asdfasdf").append("asdfasdfasdf").append("asdfasdfasdf");
String s1 = ssbb.toString();
System.out.println(s1);//abcasdfasdfasdfasdfasdfasdfasdfasdf
}
}
public class MyTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("我爱你");
//insert()王容器中指定位置插入内容,返回的还是原来那个容器
sb.insert(0, "abc").insert(2,"你好");//加到索引的前面
System.out.println(sb);//ab你好c我爱你
}
}
13.04_常见对象(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("西部开源教育科技有限公司");
sb.deleteCharAt(0);
System.out.println(sb);//部开源教育科技有限公司
//根据起始索引和终止索引,删除一段内容,返回的还是容器本身
sb.delete(0,5); //含头不含尾
System.out.println(sb);//科技有限公司
}
}
13.05_常见对象(StringBuffer的替换和反转功能)(掌握)
A:StringBuffer的替换功能
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
B:StringBuffer的反转功能
public StringBuffer reverse(): 字符串反转
C:案例演示
StringBuffer的替换和反转功能
public class MyTest2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
//根据索引删除某个字符,返回的还是容器本身
sb.append("西部开源教育科技有限公司");
//根据起始索引和终止索引,替换容器中的某一部分内容,返回的还是容器本身,含头不含尾
sb.replace(0, 3, "好好学习");//好好学习源教育科技有限公司
System.out.println(sb);
//反转容器中的内容,返回的还是容器本身
sb.reverse();
System.out.println(sb);//司公限有技科育教源习学好好
}
}
从头查找该字符串,在容器中第一次出现的索引,如果找不到就返回-1
int indexOf (String str)
从指定索引处开始查找该字符串第一次出现的索引,如果找不到就返回-1
int indexOf (String str,int fromIndex)
从后往前找
int lastIndexOf (String str)
int lastIndexOf (String str,int fromIndex)
13.06_常见对象(StringBuffer的截取功能及注意事项)(掌握)
A:StringBuffer的截取功能
public String substring(int start): 从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
B:注意事项
注意:返回值类型不再是StringBuffer本身
C:案例演示
StringBuffer的截取功能及注意事项----返回的是字符串
public class MyTest3 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcasdfasdfasdf");
String s = sb.substring(0);
//根据起始索引和终止索引,截取容器中的内容返回为字符串,含头不尾
String s1 = sb.substring(0, 3);
System.out.println(s);//abcasdfasdfasdf
System.out.println(s1);//abc
}
}
13.07_常见对象(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) {
//A:
//String-- StringBuffer
String str="abc";
//方式1 构造
StringBuffer sb = new StringBuffer(str);
//方式2: 追加
StringBuffer sb2 = new StringBuffer().append(str);
//方式3 追加
StringBuffer sb3 = new StringBuffer().insert(0, str);
//StringBuffer----String
StringBuffer sb9 = new StringBuffer("hahaha");
//方式1 重点掌握
String s = sb9.toString();
//方式2 截取
String s1 = sb9.substring(0);
//方式3: 构造方法
String s2 = new String(sb9);
}
}
13.08_常见对象(把数组转成字符串)(掌握)
A:案例演示
需求:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
输出结果:
"[1, 2, 3]"
用StringBuffer的功能实现
public class MyTest {
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 s = sb.toString();
System.out.println(s);
}
}
13.09_常见对象(字符串反转)(掌握)
A:案例演示
需求:把字符串反转
举例:键盘录入"abc"
输出结果:"cba"
用StringBuffer的功能实现
public class MyTest2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入字符串");
String s = scanner.nextLine();
String s1 = new StringBuffer(s).reverse().toString();
System.out.println(s1);
}
}
13.10_常见对象(StringBuffer和StringBuilder的区别)(掌握)
A:StringBuilder的概述
通过查看API了解一下StringBuilder类
B:面试题
String,StringBuffer,StringBuilder的区别:
StringBuffer 线程安全的长度可变字符容器,线程安全的效率低
StringBuilder 线程不安全的长度可变字符容器,线程不安全效率高
13.11_常见对象(String和StringBuffer分别作为参数传递)(掌握)
A:形式参数问题
String作为参数传递 String虽然是引用类型,但是它是一个常量,所以在做传递的时候,完全可以将其看成基本数据类型数据进行传递,String类型输入值传递
StringBuffer作为参数传递
B:案例演示
String和StringBuffer分别作为参数传递问题
public class MyTest {
public static void main(String[] args) {
//引用类型,作为参数传递,形参的改变会影响实参。
//基本类型作为参数传递,形参的改变,不影响实参。
//字符串虽说是引用类型,但是他作为参数传递,属于值传递,跟基本数据类型一致。
String str="abc";
test(str);
System.out.println(str); //2."abc"
StringBuilder sb = new StringBuilder("呵呵呵");
test(sb);
System.out.println(sb.toString()); //4.321呵呵呵
}
private static void test(StringBuilder sb) {
sb.append("123");
sb.reverse();
System.out.println(sb.toString());//3.321呵呵呵
}
private static void test(String s) {
s+="哈哈哈";
System.out.println(s); //1."abc哈哈哈"
}
}
补充:StringJoiner—拼串带格式
StringJoiner(CharSequence delimiter)
构建了一个字符容器,指定分隔符
StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
构建了一个字符容器,指定分隔符,前缀,后缀
StringJoiner add(CharSequence newElement)
增加了一份给 CharSequence值的 StringJoiner价值的下一个元素。
int length()
返回该 StringJoiner的 String表示长度。
String toString() 把容器中的数据以字符串返回
public class MyTest2 {
public static void main(String[] args) {
/* A:
案例演示
需求:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1, 2, 3};
输出结果:
"[1, 2, 3]"
用StringBuffer的功能实现*/
int[] arr = {1, 2, 3};
StringJoiner joiner = new StringJoiner(",", "[", "]");
for (int i = 0; i < arr.length; i++) {
joiner.add(arr[i] + "");
}
System.out.println(joiner.toString());//[1,2,3]
}
}
public class MyTest3 {
public static void main(String[] args) {
StringJoiner joiner = new StringJoiner(",");
joiner.add("asdfasf").add("asdfasdfasdf").add("aaaaa");
System.out.println(joiner.toString()); //asdfasf,asdfasdfasdf,aaaaa
StringJoiner joiner2 = new StringJoiner(",","(",")");
joiner2.add("asdfasf").add("asdfasdfasdf").add("aaaaa");
System.out.println(joiner2.toString()); //(asdfasf,asdfasdfasdf,aaaaa)
}
}
13.12_常见对象(数组高级冒泡排序原理图解)(掌握)
A:画图演示
需求:
数组元素:{24, 69, 80, 57, 13}
请对数组元素进行排序。
B:冒泡排序原理
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处

13.13_常见对象(数组高级冒泡排序代码实现)(掌握)
A:案例演示
数组高级冒泡排序代码
public class MyTest {
public static void main(String[] args) {
//数组的排序:通过对数组元素的比较替换移动位置等等,是一个无序序列,变成一个有序序列。
int[] arr = {24, 69, 80, 57, 13,25,69,96};
//排序算法:冒泡排序,选择排序,插入排序,快速排序,归并排序,基数排序,堆排序
//冒泡排序:元素两两比较,把最大的元素往后放,经过一轮排序 ,最大的元素,就出现在了最后面。
//ctrl+alt+M 抽取方法
// tuiDao(arr);
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));
}
private static void tuiDao(int[] arr) {
//第一轮:比4次
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));
//第二轮比3次
for (int i = 0; i < arr.length - 1 - 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));
//第三轮比2次
for (int i = 0; i < arr.length - 1 - 1 - 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));
//第三轮比1次
for (int i = 0; i < arr.length - 1 - 1 - 1 - 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));
}
}
13.14_常见对象(数组高级选择排序原理图解)(掌握)
A:画图演示
需求:
数组元素:{24, 69, 80, 57, 13}
请对数组元素进行排序。
B:选择排序原理
从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
public class MyTest {
public static void main(String[] args) {
//选择排序:每一次拿一个元素,和他后面的每一个元素,挨个比较,小的往前放,经过一轮比较,最小的元素就会出现在最前。
//如此往复,经过几轮数组就排序好了
int[] arr = {24, 69, 80, 57, 13,2,3,8,0};
//tuiDao(arr);
for (int index = 0; index <arr.length-1; index++) {
for (int i = index + 1; 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));
}
private static void tuiDao(int[] arr) {
//第一轮:从0索引处开始
int index = 0;
for (int i = 0 + 1; 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));
//第二轮:从1索引处开始
index = 1;
for (int i = 0 + index + 1; 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));
//第三轮:从2索引处开始
index = 2;
for (int i = 0 + index + 1; 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));
//第4轮:从3索引处开始
index = 3;
for (int i = 0 + index + 1; 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 = {9, 1, 2, 4, 3, 5, 7, 6, 8, 11, 10, 13, 12,-1,-2,100};
//直接插入排序:从1索引处开始,把后面的每一个元素,插入到之前的一个有序序列,使之仍保持有序。
// [9] 1, 2, 4, 3, 5, 7, 6, 8, 11, 10, 13, 12
// [1,9] 2, 4, 3, 5, 7, 6, 8, 11, 10, 13, 12
//[1,2,9] 4, 3, 5, 7, 6, 8, 11, 10, 13, 12
//[1,2,4,9] 3, 5, 7, 6, 8, 11, 10, 13, 12
// [1, 2, 3,4, 0] 9,5, 7, 6, 8, 11, 10, 13, 12
//外层循环控制轮次
for (int i = 1; i < arr.length; i++) {
//当前元素 arr[i] 他前面的元素是 arr[i-1]
//当前元素小于我前面的元素,就进行交换。
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));
}
}
快速排序
- 直接调用方法
public class MyTest {
public static void main(String[] args) {
int[] arr = {9, 1, 2, 4, 3, 5, 7, 6, 8, 11, 10, 13, 12,100,-1,-2,0,6,9,5000};
QuickSortUtils.quickSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
- 自己实现
public class QuickSortUtils {
//快速排序
public static void quickSort(int[] arr, int start, int end) {
//挖坑填数,得到基准数所在的索引位置,以这个位置分成了左右两区,然后我对左右两区进行递归调用
if (start < end) {
//获取分成左右两区基准数所在的索引位置
int index = getIndex(arr, start, end); //挖坑填数
//对左区进行递归
quickSort(arr, start, index - 1);
//对右区进行递归
quickSort(arr, index + 1, end);
}
}
//挖坑填数
/*
* 挖坑填数
1. 将基准数挖出形成第一个坑。
2.由后向前找比他小的数,找到后挖出此数填到前一个坑中。
3.由前向后找比他大或等于的数,找到后也挖出此数填到前一个坑中。
4.再重复执行2,3两步骤。
* */
private static int getIndex(int[] arr, int start, int end) {
int i = start;
int j = end;
//定义基准数
int x = arr[i];
//重复2,3步骤
while (i < j) {
//2.由后向前找比他小的数,找到后挖出此数填到前一个坑中。
while (i < j && arr[j] >= x) { //8>5 0>5
j--; //让索引往后退,找到了这个元素所在额位置
}
//找到后挖出此数填到前一个坑中。
if (i < j) {
arr[i] = arr[j];
i++; //找到之后让i顺便递增一下
}
//3. 由前向后找比他大或等于的数,找到后也挖出此数填到前一个坑中。
while (i < j && arr[i] < x) { //3<5 9<5
i++;
}
//找到后也挖出此数填到前一个坑中。
if (i < j) {
arr[j] = arr[i];
//顺便让j再减一下
j--;
}
}
//i=j
// arr[j]=x;
//把基准数填到最后一个坑中
arr[i] = x;
return i; //返回基准数所在的索引位置。
}
}
13.15_二分查找
- 普通查找法:
public class MyTest {
public static void main(String[] args) {
int[] arr = {9, 1, 2,5, 4, 3, 5, 7,100};
//基本查找:从头开始往后找
int index=getIndex(arr,100);
System.out.println(index);
}
private static int getIndex(int[] arr, int ele) {
for (int i = 0; i < arr.length; i++) {
if(ele==arr[i]){
return i;
}
}
return -1; //我们喜欢用-1 表示没有找到
}
}
- 二分法查找
A:画图演示
B:二分查找:前提数组元素必须有序
C:二分查找的思想:每一次都查中间的那个元素,比较大小就能减少一半的元素。
public class MyTest2 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,4,4,6,7,8,9,10};
//二分查找:前提是数组元素有序,原理:每次拿你找的这个元素,先从中间位置找
int index = getIndex(arr, 4);
int i = binarySearch0(arr, 0, arr.length, 4);
System.out.println(index);
System.out.println(i);
}
//二分查找
private static int getIndex(int[] arr, int ele) {
//定义三个索引
int minIndex = 0;
int maxIndex = arr.length - 1;
int centerIndex = (minIndex + maxIndex) / 2;
//查找
while (minIndex <= maxIndex) {
if (ele < arr[centerIndex]) {
maxIndex = centerIndex - 1;
} else if (ele > arr[centerIndex]) {
minIndex = centerIndex + 1;
} else {
return centerIndex;
}
//再次计算中间索引
centerIndex = (minIndex + maxIndex) / 2;
}
return -1;
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex; //最小索引
int high = toIndex - 1; //最大索引
while (low <= high) {
int mid = (low + high) >>> 1; //计算中间索引
//获取中间索引对应的元素
int midVal = a[mid];
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
return mid; // key found
}
}
return -1; // key not found.
}
}
13.18_常见对象(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 MyTest2 {
public static void main(String[] args) {
int[] arr = {9, 1, 2, 4, 3, 5, 7, 6, 8, 11, 10, 13, 12, 100, -1, -2, 0, 6, 9, 5000};
//Arrays.sort(arr);
Arrays.sort(arr, 0, 5); //你指定一段范围的元素,进行排序。
System.out.println(Arrays.toString(arr));
}
}
13.19_常见对象(Arrays类的源码解析)(掌握)
A:源码解析
public static String toString(int[] a)
```java
public class MyTest {
public static void main(String[] args) {
//Java中给我们提供了已给Arrays工具类,专门针对数组,提供了一下方法
int[] arr={1, 2, 3}; //[1,2,3]
//arr=null;
String s = Arrays.toString(arr); //把数组中的元素,拼接成一个漂亮的字符串返回。
System.out.println(s);//[1,2,3]
}
//源码
public static String toString(int[] a) {
if (a == null){
return "null";
}
int iMax = a.length - 1;
if (iMax == -1){
return "[]";
}
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax){
return b.append(']').toString();
}
b.append(", ");
}
}
}
B:源码解析
public static int binarySearch(int[] a,int key)
public class MyTest3 {
public static void main(String[] args) {
//二分查找:前提是数组元素必须有序
int[] arr = {1, 2, 3, 4, 4, 4, 6, 7, 8, 9, 10};
int i = Arrays.binarySearch(arr, 4);
System.out.println(i);
}
//源码
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex; //起始索引
int high = toIndex - 1; //最大索引
while (low <= high) {
int mid = (low + high) >>> 1; //计算中间索引
int midVal = a[mid]; //获取中间索引对应的那个元素
if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
return mid; // key found
}
}
return -(low + 1); // key not found.
}
}
比较两个数组的元素是否一致 Arrays.equals(arr, arr2);
public class MyTest4 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int[] arr2 = {1, 2, 3, 4};
//比较两个数组的元素是否一致。
boolean b = Arrays.equals(arr, arr2);
System.out.println(b);
}
//源码
public static boolean equals(int[] a, int[] a2) {
if (a == a2) {
return true;
}
if (a == null || a2 == null) {
return false;
}
int length = a.length;
if (a2.length != length) {
return false;
}
for (int i = 0; i < length; i++) {
if (a[i] != a2[i]) {
return false;
}
}
return true;
}
}
复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
static int[] copyOf ( int[] original, int newLength)
public class MyTest5 {
public static void main(String[] args) {
// static int[] copyOf ( int[] original, int newLength)复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
int[] arr = {1, 2, 3, 4,5,6,8,9};
//从旧数组0索引处开始拷贝5个元素,到一个新数组中
int[] newArr = Arrays.copyOf(arr,5);
System.out.println(Arrays.toString(newArr));
//根据起始索引和终止索引,从旧数组中拷贝一部分元素,到一个新数组中,含头不含尾。
int[] range = Arrays.copyOfRange(arr, 2, 5);
System.out.println(Arrays.toString(range));
}
}
13.20_常见对象(基本类型包装类的概述)(掌握)
A: 需求:
a:将100转换成二进制 , 八进制 , 十六进制
b:判断一个数是否在int的范围内
B:为什么会有基本类型包装类
为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型. (int a a是个变量,无法被调用方法)
C:常用操作: 常用的操作之一:用于基本数据类型与字符串之间的转换。
D:基本类型和包装类的对应
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) {
/* A:
需求:
a:
将100转换成二进制, 八进制, 十六进制
b:
判断一个数是否在int的范围内*/
//Java为了我们更加方便的对基本数据类型来操作,给我们提供了基本类型所对应的包装类型。
/*
byte------ Byte
short------ Short
int ------- Integer
long --------Long
char ------- Character
boolean ------- Boolean
float ------ Float
double ----- Double
*/
int num=100;
//不同进制的字符串的表现形式
String s = Integer.toBinaryString(num);//二进制;静态方法
System.out.println(s);//1100100
String s1 = Integer.toOctalString(num);//八进制
String s2 = Integer.toHexString(num);//十六进制
System.out.println(s1);//144
System.out.println(s2);//64
int maxValue = Integer.MAX_VALUE;//int 范围里的最大值
int minValue = Integer.MIN_VALUE;
System.out.println(maxValue);//2147483647
System.out.println(minValue);//-2147483648
}
}
13.21_常见对象(Integer类的概述和构造方法)(掌握)
A:Integer类概述
通过JDK提供的API,查看Integer类的说明
Integer 类在对象中包装了一个基本类型 int 的值,
该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
还提供了处理 int 类型时非常有用的其他一些常量和方法
B:构造方法
public Integer(int value)
public Integer(String s) //要个一个字面上是数字的字符串,如果不是就会报错
C:案例演示
使用构造方法创建对象
public class MyTest2 {
public static void main(String[] args) {
//构造方法摘要
//Integer( int value)
//构造一个新分配的 Integer 对象,它表示指定的 int 值。
//Integer(String s)
//构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
int num = 100;
Integer integer = new Integer(num);//Integer( int value)
//NumberFormatException: 数字格式化异常
//要的是字面上是一个有效数字的字符串。
Integer integer1 = new Integer("20");//Integer(String s)
}
}
13.22_常见对象(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) {
// String和int类型的相互转换
//int---String
int num=100;
String str=num+""; //"100" 方式1
String s = String.valueOf(num); //"100" 方式2
//方式3:
Integer integer = new Integer(num);
String s1 = integer.toString();
//String---int "666" ----- 666
String ss="666";
//NumberFormatException
int i = Integer.parseInt(ss); //这个字符串,字面上一定是个数字 方式1
System.out.println(i);
Integer integer1 = new Integer(ss);//方式2
int i1 = integer1.intValue();
System.out.println(i1);
}
}
13.23_常见对象(JDK5的新特性自动装箱和拆箱)(掌握)
A:JDK5的新特性
自动装箱:把基本类型转换为包装类类型
自动拆箱:把包装类类型转换为基本类型
B:案例演示
JDK5的新特性自动装箱和拆箱
public class MyTest {
public static void main(String[] args) {
//JDK1.5 新加的特性
//自动装箱:将一个基本数据类型,自动转换成他所对应的包装类型
//自动拆箱:将一个包装类型,自动转换成他所对应的基本类型。
int num=100;
//Integer integer = new Integer(num); 手动
Integer integer=num;//自动装箱
Integer aa=20;//自动装箱
Integer integer2 = new Integer(num);
int b=integer2; //自动拆箱
}
}
Integer ii = 100;
ii += 200;
public class MyTest2 {
public static void main(String[] args) {
Integer ii = 100; //自动装箱
ii += 200; //自动拆箱,自动装箱。
//手动拆装箱。
int num=100;
//手动装箱 valueOf(num)
Integer integer = Integer.valueOf(num);
//手动拆箱 intValue()
int i = integer.intValue();
System.out.println(i);
int b=20+i;
System.out.println(b);
}
}
C:注意事项
在使用时,Integer x = null;代码就会出现NullPointerException。
建议先判断是否为null,然后再使用。
13.24_常见对象(Integer的面试题)(掌握)
A:Integer的面试题
看程序写结果
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
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 i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);
System.out.println(i5.equals(i6));
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);
System.out.println(i7.equals(i8));
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; //new Integer(128)
Integer i6 = 128; //new Integer(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
}
}
public class MyTest4 {
public static void main(String[] args) {
//自动装箱 底层调用的是valueOf()来进行装箱的
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6); // false
//自动装箱
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);//true
/*
* //当我们采用自动装箱 Integer i7 = 127 这种方式 底层调用的是valueOf()来进行装箱的
*
* 在 valueOf()方法里面有这个判断,当我们装箱的值大于127 小于 -128 就会创建一个新的Integer对象返回。
*
* 如果说我们包装的这个值 在 -128 <=值<=127 之间
* 他会从 IntegerCache 这个内部类中的cache[] 数组中取一个Integer对象返回给你
*
* IntegerCache 他已经提前帮你创建好了 256个Integer对象到这个cache[]中的。
*
*
*
* */
}
/*
public static Integer valueOf(int i) { //127
if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high){
return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
}
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
*/
}
Double类型
public class MyTest {
public static void main(String[] args) {
double num=3.14;
//手动装箱
Double aDouble = Double.valueOf(num);
Double aDouble1 = new Double(num);
//手动拆箱
double v = aDouble.doubleValue();
double v1 = aDouble1.doubleValue();
Double dd=3.25;
Double ee=3.36;
Double sum=dd+ee; //自动拆箱,自动装箱
String str="0.5";
//
double v2 = Double.parseDouble(str);
long nn=20;
Long aLong = Long.valueOf(nn);
long l = aLong.longValue();
String strs="11111111111111";
Long.parseLong(strs);
String strsss="true";
Boolean aBoolean = Boolean.valueOf(strsss);
boolean b = aBoolean.booleanValue();
Boolean.parseBoolean(strsss);
}
}
public class MyTest2 {
public static void main(String[] args) {
boolean digit = Character.isDigit('a');
System.out.println(digit);
boolean a = Character.isLetter('a');
System.out.println(a);
/* static boolean isLowerCase ( char ch)
确定指定字符是否为小写字母。
static boolean isLowerCase ( int codePoint)
确定指定字符(Unicode 代码点)是否为小写字母。*/
boolean a1 = Character.isLowerCase('A');
System.out.println(a1);
boolean a2 = Character.isLowerCase(65);
System.out.println(a2);
//char----Character
/*
static boolean isWhitespace ( char ch)
确定指定字符依据 Java 标准是否为空白字符。*/
boolean b = Character.isWhitespace('a');
System.out.println(b);
}
}
13.25_day13重点总结
StringBuffer
------字符容器,可以不断的它里面追加字符
capacity;length;append(YYY)
创建了一个容器后是被重复利用的
链式编程大量拼串用append,最后toString返回一个字符串。
insert(index,YYYY)—指定索引地拼串—在indeX位置前面加上YYYY
delete(index)—删除掉特定位置的字符
delete(strartindex, endindex)—根据起始索引删除一段字符串—含头不含尾
replace(strartindex, endindex,要替换成的内容)—根据起始索引替换一段字符串—含头不含尾
reverse-----反转容器中的内容
substring-----根据起始索引和终止索引,返回为string字符串----含头不含尾
String和Stringbuffer类型;
- 正向:new(string);append;insert
- 反向:toString;substring;new(stringbuffer)
StringBuffer:线程安全效率低
StringBuilder:线程不安全效率高
引用类型作为参数传递,形参的改变会影响实参,基本类型作为参数传递,形参的改变不影响实参。
特殊:字符串虽然是引用类型,但只有他作为参数传递是属于值传递,跟基本数据类型一致。
jdk1.8 及以后:StringJoiner类:拼串
add–可以传一个参数,也可以传三个参数
toString输出
数组的排序;
通过对数组元素的比较、替换移动位置,使一个无序序列变成一个有序序列。
排序算法一共有8种
冒泡排序:元素两两比较,把最大的元素往后放。经过一轮排序,最大的元素就出现在了最后面。以此往复,进行多轮。
Ctrl+ALT+M —抽取方法
1184

被折叠的 条评论
为什么被折叠?



