Java SE day09_常用对象2
数组排序
冒泡排序
- 冒泡排序基本概念是:
- 依次比较相邻的两个数,将小数放在前面,大数放在后面。
- 即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。
- 然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,
- 直至比较最后两个数,将小数放前,大数放后。至此第一趟结束,
- 将最大的数放到了最后。在第二趟:仍从第一对数开始比较
- (因为可能由于第2个数和第3个数的交换,使得第1个数不再小于第2个数),
- 将小数放前,大数放后,一直比较到倒数第二个数(倒数第一的位置上已经是最大的),
- 第二趟结束,在倒数第二的位置上得到一个新的最大数
- (其实在整个数列中是第二大的数)。如此下去,重复以上过程,直至最终完成排序。
package cn.itcast;
/*
* 冒泡排序代码实现:
*/
//冒泡排序代码
public static void bubbleSort(int[] arr){
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}
// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}
}
选择排序:
-
从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
public static void selectSort(int[] arr){
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
案例(把字符串中的字符进行排序)
package cn.itcast_03;
/*
* 把字符串中的字符进行排序。
* 举例:"dacgebf"
* 结果:"abcdefg"
*
* 分析:
* A:定义一个字符串
* B:把字符串转换为字符数组
* C:把字符数组进行排序
* D:把排序后的字符数组转成字符串
* E:输出最后的字符串
*/
public class ArrayTest {
public static void main(String[] args) {
// 定义一个字符串
String s = "dacgebf";
// 把字符串转换为字符数组
char[] chs = s.toCharArray();
// 把字符数组进行排序
bubbleSort(chs);
//把排序后的字符数组转成字符串
String result = String.valueOf(chs);
//输出最后的字符串
System.out.println("result:"+result);
}
// 冒泡排序
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
}
二分查找(数组元素有序)
- 注意对于无序的数组应该用基本查找
package cn.itcast_04;
/*
* 查找:
* 基本查找:数组元素无序(从头找到尾)
* 二分查找(折半查找):数组元素有序
*
* 分析:
* A:定义最大索引,最小索引
* B:计算出中间索引
* C:拿中间索引的值和要查找的值进行比较
* 相等:就返回当前的中间索引
* 不相等:
* 大 左边找
* 小 右边找
* D:重新计算出中间索引
* 大 左边找
* max = mid - 1;
* 小 右边找
* min = mid + 1;
* E:回到B
*/
public class ArrayDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {11,22,33,44,55,66,77};
//写功能实现
int index = getIndex(arr, 33);
System.out.println("index:"+index);
//假如这个元素不存在后有什么现象呢?
index = getIndex(arr, 333);
System.out.println("index:"+index);
}
/*
* 两个明确:
* 返回值类型:int
* 参数列表:int[] arr,int value
*/
public static int getIndex(int[] arr,int value){
//定义最大索引,最小索引
int max = arr.length -1;
int min = 0;
//计算出中间索引
int mid = (max +min)/2;
//拿中间索引的值和要查找的值进行比较
while(arr[mid] != value){
if(arr[mid]>value){
max = mid - 1;
}else if(arr[mid]<value){
min = mid + 1;
}
//加入判断
if(min > max){
return -1;
}
mid = (max +min)/2;
}
return mid;
}
}
基本查找
针对数组无序的情况
public static int getIndex(int[] arr,int value) {
int index = -1;
for(int x=0; x<arr.length; x++) {
if(arr[x] == value) {
index = x;
break;
}
}
return index;
}
数组的Arrays工具类
Arrays:针对数组进行操作的工具类。比如说排序和查找。
- 1:public static String toString(int[] a) 把数组转成字符串
- 2:public static void sort(int[] a) 对数组进行排序
- 3:public static int binarySearch(int[] a,int key) 二分查找
package cn.itcast_05;
import java.util.Arrays;
/*
* Arrays:针对数组进行操作的工具类。比如说排序和查找。
* 1:public static String toString(int[] a) 把数组转成字符串
* 2:public static void sort(int[] a) 对数组进行排序
* 3:public static int binarySearch(int[] a,int key) 二分查找
*/
public class ArraysDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
// public static String toString(int[] a) 把数组转成字符串
System.out.println("排序前:" + Arrays.toString(arr));
// public static void sort(int[] a) 对数组进行排序
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));
// [13, 24, 57, 69, 80]
// public static int binarySearch(int[] a,int key) 二分查找
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
}
}
Integer(掌握)
-
(1)为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型。包装类类型。
-
基本类型 | 引用类型
-
byte | Byte
-
short | Short
-
int | Integer
-
long | Long
-
float | Float
-
double | Double
-
char | Character
-
boolean | Boolean
-
用于基本数据类型与字符串之间的转换。
-
需求1:我要求大家把100这个数据的二进制,八进制,十六进制计算出来
-
需求2:我要求大家判断一个数据是否是int范围内的。
-
首先你的知道int的范围是多大?
public class IntegerDemo {
public static void main(String[] args) {
// 不麻烦的就来了
// public static String toBinaryString(int i)
//二进制
System.out.println(Integer.toBinaryString(100));
// public static String toOctalString(int i)
//10进制
System.out.println(Integer.toOctalString(100));
// public static String toHexString(int i)
//16进制
System.out.println(Integer.toHexString(100));
// public static final int MAX_VALUE
System.out.println(Integer.MAX_VALUE);
// public static final int MIN_VALUE
System.out.println(Integer.MIN_VALUE);
}
}
(2)Integer的构造方法
A:Integer i = new Integer(100);
B:Integer i = new Integer("100");
注意:这里的字符串必须是由数字字符组成
(3)String和int的相互转换
A:String -- int
Integer.parseInt("100");
B:int -- String
String.valueOf(100);
(4)常用的基本进制转换
- public static String toBinaryString(int i)
- public static String toOctalString(int i)
- public static String toHexString(int i)
- 十进制到其他进制
- public static String toString(int i,int radix)
- 由这个我们也看到了进制的范围:2-36
- 为什么呢?0,…9,a…z
- 其他进制到十进制
- public static int parseInt(String s,int radix)
(5)JDK5的新特性
- 自动装箱:把基本类型转换为包装类类型
- 自动拆箱:把包装类类型转换为基本类型
- 注意一个小问题:
- 在使用时,Integer x = null;代码就会出现NullPointerException。
- 建议先判断是否为null,然后再使用。
public class IntegerDemo {
public static void main(String[] args) {
// 定义了一个int类型的包装类类型变量i
// Integer i = new Integer(100);
Integer ii = 100;
ii += 200;
System.out.println("ii:" + ii);
// 通过反编译后的代码
// Integer ii = Integer.valueOf(100); //自动装箱
// ii = Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱
// System.out.println((new StringBuilder("ii:")).append(ii).toString());
Integer iii = null;
// NullPointerException
if (iii != null) {
iii += 1000;
System.out.println(iii);
}
}
}
(6)面试题
-128到127之间的数据缓冲池问题
- 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
- Integer ii = Integer.valueOf(127);
Character(了解)
(1)Character构造方法
Character ch = new Character(‘a’);
要掌握的方法:
- public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
- public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
- public static boolean isDigit(char ch):判断给定的字符是否是数字字符
- public static char toUpperCase(char ch):把给定的字符转换为大写字符
- public static char toLowerCase(char ch):把给定的字符转换为小写字符