冒泡排序 :轻的上浮,沉的降低java
原理:两个相邻位置比较,若是前面的比后面的元素大,就交换位置数组
选择排序:用一个索引位置上的元素,依次与其余位置上的元素比较,小的在前,大的在后安全
package com.heima.array; public class Demo1_Array { /** * * A:案例演示 * 数组高级冒泡排序代码 */ public static void main(String[] args) { int[] arr = {24, 69, 80, 57, 13}; bubbleSort(arr); //selectSort(arr); print(arr); } /* * 冒泡排序 * 1,返回值类型,void * 2,参数列表,int[] arr * * 第一次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3],arr[3]与arr[4]比较4次 第二次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3]比较3次 第三次:arr[0]与arr[1],arr[1]与arr[2]比较2次 第四次:arr[0]与arr[1]比较1次 */ public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { //外循环只须要比较arr.length-1次就能够了 for (int j = 0; j < arr.length - 1 - i; j++) { //-1为了防止索引越界,-i为了提升效率 if(arr[j] > arr[j+1]) { /*int temp = arr[j]; arr[j] = arr[j + 1]; arr[j+1] = temp;*/ swap(arr,j,j+1); } } } } /* * 打印数组 * 1,返回值类型void * 2,参数列表int[]arr */ public static void print(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } /* * 选择排序 * 1,返回值类型void * 2,参数列表int[] arr * * 第一次:arr[0]分别与arr[1-4]比较,比较4次 第二次:arr[1]分别与arr[2-4]比较,比较3次 第三次:arr[2]分别与arr[3-4]比较,比较2次 第四次:arr[3]与arr[4]比较,比较1次 */ public static void selectSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { //只须要比较arr.length-1次 for (int j = i + 1; j < arr.length; j++) { if(arr[i] > arr[j]) { /*int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;*/ swap(arr,i,j); } } } } /* * 换位操做 * 1,返回值类型,void * 2,参数列表int[] arr.int i,int j * * 若是某个方法,只针对本类使用,不想让其余类使用就能够定义成私有的 */ private static void swap(int[] arr,int i,int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
二分查找法app
package com.heima.array; public class Demo2_Array { /** * * A:案例演示 * 数组高级二分查找代码 * B:注意事项 * 若是数组无序,就不能使用二分查找。 * 由于若是你排序了,可是你排序的时候已经改变了我最原始的元素索引。 */ public static void main(String[] args) { int[] arr = {11,22,33,44,55,66,77}; System.out.println(getIndex(arr, 22)); System.out.println(getIndex(arr, 66)); System.out.println(getIndex(arr, 88)); } /* * 二分查找 * 1,返回值类型,int * 2,参数列表int[] arr,int value */ public static int getIndex(int[] arr, int value) { int min = 0; int max = arr.length - 1; int mid = (min + max) / 2; while(arr[mid] != value) { //当中间值不等于要找的值,就开始循环查找 if(arr[mid] < value) { //当中间值小于了要找的值 min = mid + 1; //最小的索引改变 }else if (arr[mid] > value){ //当中间值大于了要找的值 max = mid - 1; //最大的索引改变 } mid = (min + max) / 2; //不管最大仍是最小改变,中间索引都会随之改变 if(min > max) { //若是最小索引大于了最大索引,就没有查找的可能性了 return -1; //返回-1 } } return mid; } }
package com.heima.array; import java.util.Arrays; public class Demo3_Arrays { /** * public static String toString(int[] a) * public static void sort(int[] a) * public static int binarySearch(int[] a,int key) * * public static String toString(int[] a) { if (a == null) //若是传入的数组是null return "null"; //返回null int iMax = a.length - 1; //iMax最大索引 if (iMax == -1) //若是数组中没有元素 return "[]"; //返回[] StringBuilder b = new StringBuilder(); //线程不安全,效率高 b.append('['); //将[添加到字符串缓冲区中 for (int i = 0; ; i++) { //遍历数组,判断语句没有写默认是true b.append(a[i]); //把第一个元素添加进字符串缓冲区 if (i == iMax) //若是索引等于了最大索引值 return b.append(']').toString(); //将]添加到字符串缓冲区,在转换成字符串并返回 b.append(", "); //若是不等于最大索引就将, 添加到缓冲区 } } private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) { int low = fromIndex; //最小索引0 int high = toIndex - 1; //最大索引数组长度-1 while (low <= high) { //最小索引小于等于最大索引能够循环判断 int mid = (low + high) >>> 1; //求出中间索引值,(最小+最大)/2 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.//-插入点 - 1 } */ public static void main(String[] args) { int[] arr = {33,22,11,44,66,55}; System.out.println(Arrays.toString(arr)); //数组转字符串 Arrays.sort(arr); //排序 System.out.println(Arrays.toString(arr)); int[] arr2 = {11,22,33,44,55,66}; System.out.println(Arrays.binarySearch(arr2, 22)); System.out.println(Arrays.binarySearch(arr2, 66)); System.out.println(Arrays.binarySearch(arr2, 9)); //-插入点-1 } }
A:StringBuffer的构造方法:
* public StringBuffer():无参构造方法
* public StringBuffer(int capacity):指定容量的字符串缓冲区对象
* public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
* B:StringBuffer的方法:
* public int capacity():返回当前容量。 理论值(不掌握)
* public int length():返回长度(字符数)。 实际值
* C:案例演示
* 构造方法和长度方法的使用 dom
package com.heima.stringbuffer; public class Demo1_StringBuffer { /** * * A:StringBuffer的构造方法: * public StringBuffer():无参构造方法 * public StringBuffer(int capacity):指定容量的字符串缓冲区对象 * public StringBuffer(String str):指定字符串内容的字符串缓冲区对象 * B:StringBuffer的方法: * public int capacity():返回当前容量。 理论值(不掌握) * public int length():返回长度(字符数)。 实际值 * C:案例演示 * 构造方法和长度方法的使用 */ public static void main(String[] args) { StringBuffer sb = new StringBuffer(); System.out.println(sb.length()); //容器中的字符个数,实际值 System.out.println(sb.capacity()); //容器的初始容量,理论值 StringBuffer sb2 = new StringBuffer(10); System.out.println(sb2.length()); System.out.println(sb2.capacity()); StringBuffer sb3 = new StringBuffer("heima"); System.out.println(sb3.length()); //实际字符的个数 System.out.println(sb3.capacity()); //字符串的length + 初始容量 } }
A:StringBuffer的添加功能
* public StringBuffer append(String str):
* 能够把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区自己
* public StringBuffer insert(int offset,String str):
* 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区自己
StringBuffer是字符串缓冲区,当new的时候是在堆内存建立了一个对象,底层是一个长度为16的字符数组
当调用添加的方法时,不会再从新建立对象,在不断向原缓冲区添加字符ui
以下代码,String初始化赋值之后再进行赋值操做,原来的对象变成了垃圾,而StringBuffer则四个引用指向的是同一个对象(sb,sb1,sb2,sb3四个引用均指向同一个对象)spa
package com.heima.stringbuffer; public class Demo2_StringBuffer { /** * * A:StringBuffer的添加功能 * public StringBuffer append(String str): * 能够把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区自己 * public StringBuffer insert(int offset,String str): * 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区自己 StringBuffer是字符串缓冲区,当new的时候是在堆内存建立了一个对象,底层是一个长度为16的字符数组 当调用添加的方法时,不会再从新建立对象,在不断向原缓冲区添加字符 */ public static void main(String[] args) { //demo1(); StringBuffer sb = new StringBuffer("1234"); sb.insert(3, "heima"); //在指定位置添加元素,若是没有指定位置的索引就会报索引越界异常 System.out.println(sb); } private static void demo1() { StringBuffer sb = new StringBuffer(); StringBuffer sb2 = sb.append(true); StringBuffer sb3 = sb.append("heima"); StringBuffer sb4 = sb.append(100); System.out.println(sb.toString()); //StringBuffer类中重写了toString方法,显示的是对象中的属性值 System.out.println(sb2.toString()); System.out.println(sb3.toString()); System.out.println(sb4.toString()); } }
A:StringBuffer的删除功能
* public StringBuffer deleteCharAt(int index):
* 删除指定位置的字符,并返回自己
* public StringBuffer delete(int start,int end):
* 删除从指定位置开始指定位置结束的内容,并返回自己
线程
package com.heima.stringbuffer; public class Demo3_StringBuffer { /** * * A:StringBuffer的删除功能 * public StringBuffer deleteCharAt(int index): * 删除指定位置的字符,并返回自己 * public StringBuffer delete(int start,int end): * 删除从指定位置开始指定位置结束的内容,并返回自己 */ public static void main(String[] args) { StringBuffer sb = new StringBuffer(); //sb.deleteCharAt(5); //当缓冲区中这个索引上没有元素的时候就会报StringIndexOutOfBoundsException sb.append("heima"); //sb.deleteCharAt(4); //根据索引删除掉索引位置上对应的字符 //sb.delete(0, 2); //删除的时候是包含头,不包含尾 //System.out.println(sb); //sb.delete(0, sb.length()); //清空缓冲区 //System.out.println(sb); sb = new StringBuffer(); //不要用这种方式清空缓冲区,原来的会变成垃圾,浪费内存 System.out.println(sb); } }
A:StringBuffer的替换功能
* public StringBuffer replace(int start,int end,String str):
* 从start开始到end用str替换
* B:StringBuffer的反转功能
* public StringBuffer reverse():
* 字符串反转指针
package com.heima.stringbuffer; public class Demo4_StringBufferMethod { /** * * A:StringBuffer的替换功能 * public StringBuffer replace(int start,int end,String str): * 从start开始到end用str替换 * B:StringBuffer的反转功能 * public StringBuffer reverse(): * 字符串反转 */ public static void main(String[] args) { StringBuffer sb = new StringBuffer("我爱总复习"); //sb.replace(0, 3, "bai"); //替换 //System.out.println(sb); sb.reverse(); System.out.println(sb); } }
A:StringBuffer的截取功能
* public String substring(int start):
* 从指定位置截取到末尾
* public String substring(int start,int end):
* 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
* B:注意事项
* 注意:返回值类型再也不是StringBuffer自己
code
package com.heima.stringbuffer; public class Demo5_StringBufferMethod { /** * * A:StringBuffer的截取功能 * public String substring(int start): * 从指定位置截取到末尾 * public String substring(int start,int end): * 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置 * B:注意事项 * 注意:返回值类型再也不是StringBuffer自己 */ public static void main(String[] args) { StringBuffer sb = new StringBuffer("woaiheima"); //String str = sb.substring(4); //System.out.println(str); //System.out.println(sb); String str3 = sb.substring(4, 7); System.out.println(str3); } }
A:String -- StringBuffer
* a:经过构造方法
* b:经过append()方法
* B:StringBuffer -- String
* a:经过构造方法
* b:经过toString()方法
* c:经过subString(0,length);
package com.heima.stringbuffer; public class Demo6_StringBuffer { /** * * A:String -- StringBuffer * a:经过构造方法 * b:经过append()方法 * B:StringBuffer -- String * a:经过构造方法 * b:经过toString()方法:经常使用 * c:经过subString(0,length); */ public static void main(String[] args) { //demo1(); StringBuffer sb = new StringBuffer("heima"); String s1 = new String(sb); //经过构造将StringBuffer转换为String System.out.println(s1); String s2 = sb.toString(); //经过toString方法将StringBuffer转换为String System.out.println(s2); String s3 = sb.substring(0, sb.length()); //经过截取子字符串将StringBuffer转换为String System.out.println(s3); } private static void demo1() { StringBuffer sb1 = new StringBuffer("heima"); //经过构造方法将字符串转换为StringBuffer对象 System.out.println(sb1); StringBuffer sb2 = new StringBuffer(); sb2.append("heima"); //经过append方法将字符串转换为StringBuffer对象 System.out.println(sb2); } }
A:形式参数问题
* String做为参数传递
* StringBuffer做为参数传递
* B:案例演示
* String和StringBuffer分别做为参数传递问题
*
基本数据类型的值传递,不改变其值
引用数据类型的值传递,改变其值
String类虽然是引用数据类型,可是他看成参数传递时和基本数据类型是同样的
package com.heima.stringbuffer; public class Demo7_StringBuffer { /** * * A:形式参数问题 * String做为参数传递 * StringBuffer做为参数传递 * B:案例演示 * String和StringBuffer分别做为参数传递问题 * 基本数据类型的值传递,不改变其值 引用数据类型的值传递,改变其值 String类虽然是引用数据类型,可是他看成参数传递时和基本数据类型是同样的。初始化不会改变 */ public static void main(String[] args) { String s = "heima"; System.out.println(s); //heima change(s); System.out.println(s); //heima。change方法弹栈,heimaitcast消失,存在的仍是heima System.out.println("---------------------"); StringBuffer sb = new StringBuffer(); sb.append("heima"); System.out.println(sb); change(sb); System.out.println(sb); } public static void change(StringBuffer sb) { sb.append("itcast"); } public static void change(String s) { s += "itcast"; } }
需求:把数组中的数据按照指定个格式拼接成一个字符串
*
举例:
int[] arr = {1,2,3};
输出结果:
"[1, 2, 3]"
用StringBuffer的功能实现
package com.heima.test; public class Test1 { /** * * 需求:把数组中的数据按照指定个格式拼接成一个字符串 * 举例: int[] arr = {1,2,3}; 输出结果: "[1, 2, 3]" 用StringBuffer的功能实现 */ public static void main(String[] args) { int[] arr = {1,2,3}; System.out.println(arrayToString(arr)); } /* * 将数组转换为字符串 * 1,返回值类型String * 2,参数列表int[] * * arrayToString 将数组转换为字符串 * array2String 2与to的发音同样就用2替换了to,后来演变成了一种书写习惯 * dom4j domForJ */ public static String arrayToString(int[] arr) { StringBuffer sb = new StringBuffer(); //建立字符串缓冲区对象 sb.append("["); //将[添加到缓冲区 //{1,2,3} for (int i = 0; i < arr.length; i++) { //遍历数组 //sb.append(arr[i] + ", "); //这样作没有]。不推荐这种方式链接字符串,应为变量与常量相加时候,底层会建立新的String if(i == arr.length - 1) { sb.append(arr[i]).append("]"); //[1, 2, 3] }else { sb.append(arr[i]).append(", "); //[1, 2, } } return sb.toString(); } }
A:案例演示
*
需求:把字符串反转
举例:键盘录入"abc"
输出结果:"cba"
用StringBuffer的功能实现
package com.heima.test; import java.util.Scanner; public class Test2 { /** * * A:案例演示 * 需求:把字符串反转 举例:键盘录入"abc" 输出结果:"cba" 用StringBuffer的功能实现 */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); //建立键盘录入对象 String line = sc.nextLine(); //将键盘录入的字符串存储在line中 /*StringBuffer sb = new StringBuffer(line); //将字符串转换为StringBuffer对象 sb.reverse(); //将缓冲区的内容反转 System.out.println(sb.toString());*/ System.out.println(revString(line)); } /* * 将字符串反转 * 1,返回值类型String * 2,参数列表String line */ public static String revString(String line) { StringBuffer sb = new StringBuffer(line); //将字符串转换为StringBuffer对象 sb.reverse(); //将缓冲区的内容反转 return sb.toString(); } }
StringBuffer是JDK1.0版本的,是线程安全的,效率低
StringBuilder是JDK1.5版本的,是线程不安全的,效率高
String是一个不可变字符序列
StringBuffer,StringBuilder是可变的字符序列
A:为何会有基本类型包装类
* 将基本数据类型封装成对象的好处在于能够在对象中定义更多的功能方法操做该数据。
* B:经常使用操做
* 经常使用的操做之一:用于基本数据类型与字符串之间的转换。
* C:基本类型和包装类的对应
package com.heima.wrapclass; public class Demo1_Integer { /** * * A:为何会有基本类型包装类 * 将基本数据类型封装成对象的好处在于能够在对象中定义更多的功能方法操做该数据。 * B:经常使用操做 * 经常使用的操做之一:用于基本数据类型与字符串之间的转换。 * C:基本类型和包装类的对应 * byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean */ public static void main(String[] args) { System.out.println(Integer.toBinaryString(60)); System.out.println(Integer.toOctalString(60)); System.out.println(Integer.toHexString(60)); } }
A:Integer类概述
* 经过JDK提供的API,查看Integer类的说明
* Integer 类在对象中包装了一个基本类型 int 的值,
* 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
* 还提供了处理 int 类型时很是有用的其余一些常量和方法
* B:构造方法
* public Integer(int value)
* public Integer(String s)
* C:案例演示
* 使用构造方法建立对象
package com.heima.wrapclass; public class Demo2_Integer { /** * * A:Integer类概述 * 经过JDK提供的API,查看Integer类的说明 * Integer 类在对象中包装了一个基本类型 int 的值, * 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换, * 还提供了处理 int 类型时很是有用的其余一些常量和方法 * B:构造方法 * public Integer(int value) * public Integer(String s) * C:案例演示 * 使用构造方法建立对象 */ public static void main(String[] args) { System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); Integer i1 = new Integer(100); System.out.println(i1); //Integer i2 = new Integer("abc"); //java.lang.NumberFormatException数字格式异常 //System.out.println(i2); //由于abc不是数字字符串,因此转换会报错 Integer i3 = new Integer("100"); System.out.println(i3); } }
A:int -- String
* a:和""进行拼接
* b:public static String valueOf(int i)
* c:int -- Integer -- String(Integer类的toString方法())
* d:public static String toString(int i)(Integer类的静态方法)
* B:String -- int
* a:String -- Integer -- int
* public static int parseInt(String s)
基本数据类型包装类有八种,其中七种都有parseXxx的方法,能够将这七种的字符串表现形式转换成基本数据类型
package com.heima.wrapclass; public class Demo3_Integer { /** * * A:int -- String * a:和""进行拼接 * b:public static String valueOf(int i) * c:int -- Integer -- String(Integer类的toString方法()) * d:public static String toString(int i)(Integer类的静态方法) * B:String -- int * a:String -- Integer -- int * public static int parseInt(String s) 基本数据类型包装类有八种,其中七种都有parseXxx的方法,能够将这七种的字符串表现形式转换成基本数据类型 */ public static void main(String[] args) { //demo1(); String s1 = "true"; boolean b = Boolean.parseBoolean(s1); System.out.println(b); //String s2 = "abc"; //char c = Character.p //char的包装类Character中没有pareseXxx的方法 //字符串到字符的转换经过toCharArray()就能够把字符串转换为字符数组 } private static void demo1() { //int ----> String int转换成String int i = 100; String s1 = i + ""; //推荐用 String s2 = String.valueOf(i); //推荐用 Integer i2 = new Integer(i); String s3 = i2.toString(); String s4 = Integer.toString(i); System.out.println(s1); //String----> int String 转换int String s = "200"; Integer i3 = new Integer(s); int i4 = i3.intValue(); //将Integer转换成了int数 int i5 = Integer.parseInt(s); //将String转换为int,推荐用这种 } }
A:JDK5的新特性
* 自动装箱:把基本类型转换为包装类类型
* 自动拆箱:把包装类类型转换为基本类型
* B:案例演示
* JDK5的新特性自动装箱和拆箱
* Integer ii = 100;
* ii += 200;
* C:注意事项
* 在使用时,Integer x = null;代码就会出现NullPointerException。
* 建议先判断是否为null,而后再使用。
package com.heima.wrapclass; public class Demo4_JDK5 { /** * * A:JDK5的新特性 * 自动装箱:把基本类型转换为包装类类型 * 自动拆箱:把包装类类型转换为基本类型 * B:案例演示 * JDK5的新特性自动装箱和拆箱 * Integer ii = 100; * ii += 200; * C:注意事项 * 在使用时,Integer x = null;代码就会出现NullPointerException。 * 建议先判断是否为null,而后再使用。 */ public static void main(String[] args) { // int x = 100; // Integer i1 = new Integer(x); //将基本数据类型包装成对象,装箱 // // int y = i1.intValue(); //将对象转换为基本数据类型,拆箱 Integer i2 = 100; //自动装箱,把基本数据类型转换成对象 int z = i2 + 200; //自动拆箱,把对象转换为基本数据类型 System.out.println(z); Integer i3 = null; int a = i3 + 100; //底层用i3调用intValue,可是i3是null,null调用方法就会出现 System.out.println(a); //空指针异常java.lang.NullPointerException } }
package com.heima.wrapclass; public class Demo5_Integer { /** * @param args */ public static void main(String[] args) { Integer i1 = new Integer(97); Integer i2 = new Integer(97); System.out.println(i1 == i2); //false System.out.println(i1.equals(i2)); //true System.out.println("-----------"); Integer i3 = new Integer(197); Integer i4 = new Integer(197); System.out.println(i3 == i4); //false System.out.println(i3.equals(i4)); //true System.out.println("-----------"); Integer i5 = 127; Integer i6 = 127; System.out.println(i5 == i6); //true System.out.println(i5.equals(i6)); //true System.out.println("-----------"); Integer i7 = 128; Integer i8 = 128; System.out.println(i7 == i8); System.out.println(i7.equals(i8)); //true /* * -128到127是byte的取值范围,若是在这个取值范围内,自动装箱就不会新建立对象,而是从常量池中获取 * 若是超过了byte取值范围就会再新建立对象 * * public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) //i>= -128 && i <= 127 return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } */ } }