工具类
Arrays类的概述
一.概述
- 针对数组操作的工具类
- 提供了排序查找等功能
二.成员方法
- public static String toString(int[] a):将数组转换为字符串
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(", "); //如果不等于最大索引值,添加到缓冲区
}
}
- public static void sort(int[] a):对数组进行排序
这里的排序是快速排序
- public static int binarySearch(int[] arr,int key):二分查找
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; //>>> 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. //没找到
}
基本类型包装类
一.为什么会有基本类型包装类?
- 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
二.常用操作
- 用于基本数据类型之间的转换
三.基本类型和包装类对应
- byte -----------> Byte
- short ----------> Short
- int -------------> Integer
- long -----------> Long
- float ----------> Float
- double -------->Double
- char ----------> Character
- boolean -------> Boolean
案例演示
public class Demo1_Integer {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(60)); //转为二进制字符串
System.out.println(Integer.toOctalString(60)); //转为八进制字符串
System.out.println(Integer.toHexString(60)); //转为十六进制字符串
}
}
Integer类
一.概述
- 通过JDK提供的API,查看Integer类的说明
- Integer类在对象中包装了一个基本类型int的值
- 该类提供多个方法,能在int类型和String类型之间互相转换
- 还提供了处理int类型时非常有用的其他常量和方法
二.构造方法
- public Integer(int value)
- public Integer(String s)
案例演示
public class Demo2_Integer {
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);//字符串100
//Integer i2 = new Integer("abc"); //java.lang.NumberFormatException数字格式异常
//System.out.println(i2);
Integer i3 = new Integer("100");
System.out.println(i3);//数字100
}
}
String类与int类的相互转换
一.int --> String
- 用 + 和 “” 进行拼凑
- String类的valueOf()方法:public static String valueOf(int i)
- 先转成Integer然后用Integer类中的toString()方法:public String toString()
- 直接用Integer类中的toString(int i)方法:public static String toString(int i)
二.String --> int
- 先将字符串转为Integer然后将通过Integer的intValue()方法转换成int数:public int intValue()
- 将String通过String类的parseInt(String s)方法转换为int:public static int parseInt(String s),推荐
基本数据类型包装类有八种,其中七种都有parseXxx的方法,可以将这七种的字符串的表现形式转换
成基本数据类型
char 的包装类Character中没有parseXxx方法字符串转换为字符的转换通过toCharArray()实现的
案例演示
public class Demo2_Integer2 {
public static void main(String[] args) {
//demo1();
//demo2();
}
private static void demo2() {
String s1 = "true";
boolean b = Boolean.parseBoolean(s1);
System.out.println(b);
}
private static void demo1() {
//int --> String
int a = 100;
String s1 = a + ""; //拼凑
System.out.println(s1);
String s2 = String.valueOf(a); //String类的valueOf()方法
System.out.println(s2);
Integer i = new Integer(a); //先转成Integer然后用Integer类中的toString()方法
String s3 = i.toString();
System.out.println(s3);
String s4 = Integer.toString(a); //直接用Integer类中的toString(int i)方法
System.out.println(s4);
//String --> int
String s = "200";
Integer i2 = new Integer(s);
int s5 = i2.intValue(); //先将字符串转为Integer然后将Integer转换成int数
System.out.println(s5);
int s6 = Integer.parseInt(s); //将String转换为int,推荐
System.out.println(s6);
}
}
JDK5的新特性
一.特性
- 自动装箱:把基本数据类型转换为包装类类型
- 自动拆箱:把包装类数据类型转换为基本类型
二.注意事项
- 在使用时,Integer x = null;代码就会出现NUllPointerException,建议先判断是否为null,然后再使用
案例演示
public class Demo3_JDK5 {
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
}
}
面试题
-128到127是byte的取值范围,如果在这个范围内,自动装箱就不会创建新对象,而是从常量池中获取
如果超过了byte的取值范围就会重新创建对象
public class Demo2_Integer3 {
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
Integer i3 = new Integer(197);
Integer i4 = new Integer(197);
System.out.println(i3 == i4); //false
System.out.println(i3.equals(i4)); //true
Integer i5 = 127;
Integer i6 = 127; //127在byte范围内所以
System.out.println(i5 == i6); //true
System.out.println(i5.equals(i6)); //true
Integer i7 = 128;
Integer i8 = 128; //128不在byte范围内
System.out.println(i7 == i8); //false
System.out.println(i7.equals(i8)); //true
/*
-128到127是byte的取值范围,如果在这个范围内,自动装箱就不会创建新对象,而是从常量池中获取
如果超过了byte的取值范围就会重新创建对象
*/
}
}