基本类型包装类
基本类型包装类概述
java将基本数据类型值封装成了对象。可以提供更多的操作基本数值的功能。
Integer类的概述
* * 为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型即包装类。
* byte Byte
* short Short
* int Integer
* long Long
* float Float
* double Double
* char Character
* boolean Boolean
* 常用的操作之一:用于基本数据类型和字符串之间的相互转换
*1、 Integer类的构造方法:
public Integer(int value)
public Integer(String s)
注意:这个字符串必须是由数字字符组成。
2、常用的基本进制转换
public static String toBinaryString(int i)
public static String toOctalString(int i)
public static String toHexString(int i)
*3、十进制转换成其他进制
public static String toString(int i,int radix)
有这个我们看到了进制radix的取值范围:2-36
为什么呢?0,...9,a,...z (10+26)
其他进制到十进制
public static int parseInt(String s,int radix)
* int类型和String类型的相符转换
* int -- String
* String.valueOf(number);
* String -- int
* Integer.parseInt(s);
面试题:
/*
* 看程序写结果
* 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据。
*/
public class 面试题 {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1==i2);//false
System.out.println(i1.equals(i2));//true
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3==i4);//false
System.out.println(i3.equals(i4));//true
Integer i5 = 127;
Integer i6 = 127;
System.out.println(i5==i6);//true
System.out.println(i5.equals(i6));//true
Integer i7 = 128;
Integer i8 = 128;
System.out.println(i7==i8);//false
System.out.println(i7.equals(i8));//true
//Integer ii = Integer.valueOf(100);//自动装箱
//通过查看源码,我们就知道了,针对-128到127之间的数据,
//做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间。
}
}
自动装箱拆箱
自动拆箱:对象转成基本类型
自动装箱:基本类型转成对象
JDK5的新特性
* JDK5的新特性
* 自动装箱:把基本数据类型转换为包装类类型
* 自动拆箱:把包装类类型转换为基本类型
*
* 注意一个小问题:
* 在使用时,Integer x = null;代码就会出现NullPointerException。
* 建议先判断是否为null,然后再使用。
*
* //定义了一个int类型的包装类类型变量i
//Integer i = new Integer(100);
Integer ii = 100; //自动装箱
ii +=200; //自动拆箱+自动装箱
System.out.println(ii);//300
//通过反编译后的代码
// Integer ii = Integer.valueOf(100);//自动装箱
// ii = Integer.valueOf(ii.intValue() + 200);//自动拆箱,自动装箱
// System.out.println((new StringBuilder("ii")).append(ii).toString());
Random类
* Random:产生随机数的类
* 构造方法:
* public Random()没有给种子,用的是默认种子,是当前时间的毫秒值。
* public Random(int seed)给出指定的种子
* 给定种子后,每次得到的随机数是相同的
* 成员方法
* public int nextInt():返回的是int范围内的随机数
* public int nextInt(long n):返回的是[0,n)范围内的随机数
System类概述
* public final class System extends Object
* System 类包含一些有用的类字段和方法。它不能被实例化。
*
* 成员方法
* public static void gc()运行垃圾回收器。
* public static void exit(int status)终止当前正在运行的 Java 虚拟机。
* 参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
* public static long currentTimeMillis()返回以毫秒为单位的当前时间。
* public static void arraycopy(Object src, int srcPos,Object dest,int destPos,int length)
* 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
* * 参数:
* src - 源数组。
* srcPos - 源数组中的起始位置。
* dest - 目标数组。
* destPos - 目标数据中的起始位置。
* length - 要复制的数组元素的数量。
* Math类概述和常用方法
* public final class Math extends Object
* Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
* 成员变量
* public static final double PI
* public static final double E
* 成员方法:
* public static int abs(int a) 绝对值。
* public static double ceil(double a) 向上取整
* public static double floor(double a) 向下取整
* public static double max(double a,double b) 最大值
* public static double min(double a,double b) 最小值
* public static double pow(double a,double b) a的b次幂
* public static double random() 随机数[0.0,1.0)
* public static long round(double a) 四舍五入(原理是:(原数+0.5)=结果向下取整)
* public static double sqrt(double a) 正平方根
*
* 需求:请设计一个方法,可以实现获取任意范围内的随机数。
* public static int getRandom(int start,int end){
* int random = (int)(Math.random()*(end-start+1))+start;
* return random;
* }
* Arrays:针对数组进行操作的工具类,比如说排序和查找
* 1.public static String toString(int[] a)可以让任意类型的数组转换成字符串,拿int类型数组举例。
* 2.public static void sort(int[] a)可以让任意类型的数组进行排序,拿int类型数组举例。//sort底层使用快速排序(了解即可)
* 3.public static int binarySearch(int[] a,int key)可以让任意类型的数组进行二分查找,拿int类型数组举例。
排序与查找
-
排序
- 数组元素冒泡排序
相邻的元素值依次比较,把大的值放后面的元素中,
数组循环一圈后,则把最大元素值互换到了最后一个
元素中。数组再循环一圈后,把第二大的元素值互换
到了倒数第二个元素中。按照这种方式,数组循环多
圈以后,就完成了数组元素的排序。这种排序方式我
们称为冒泡排序。
//冒泡排序算法
public static void bubbleSort6(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
- 数组元素选择排序
用要比较的第一个元素与该元素后面的数组元素依次
比较到数组的最后一个元素,把小的值放在第一个数
组元素中,数组循环一圈后,则把最小元素值互换到
了第一个元素中。数组再循环一圈后,把第二小的元
素值互换到了第二个元素中。按照这种方式,数组循
环多圈以后,就完成了数组元素的排序。这种排序方
式我们称为选择排序。
// 选择排序算法
public static void selectSort6(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[i]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
-
查找
数组元素二分查找(折半查找)
最中间位置的元素值与要查找的指定数值进行比较,
若不相等,则根据比较的结果,缩小查询范围为上次
数组查询范围的一半;再根据新的查询范围,更新最
中间元素位置,然后使用中间元素值与要查找的指定
数值进行比较.
/*
* 基本查找:数组元素无序(从头找到尾)
* 二分查找(折半查找):数组元素有序
* 前提:数组必须是有序的。
* 思想:每次都猜中间的那个元素,比较大或者小,就能减少一半的元素。
* 思路:
* A:定义最小索引,最大索引
* B:计算出中间索引
* C:拿中间索引的值和要查找的元素进行比较
* 相等:就直接返回当前的中间索引
* 不相等:
* 大了:在左边找
* 小了:在右边找
* D:重新获取最小索引或者最大索引
* 大了 在左边找
* max = mid -1;
* 小了 在右边找
* min = mid+1;
* E:回到B的位置。
*/
public class 二分查找 {
public static void main(String[] args) {
int[] arr = { 11, 22, 33, 44, 55, 66, 77, 88 };
//两端情况
int index = getIndex(arr, 88);
System.out.println(index);
System.out.println("====================");
//中间情况
int index2 = getIndex(arr, 33);
System.out.println(index2);
System.out.println("====================");
//不存在情况
int index3 = getIndex(arr, 333);
System.out.println(index3);
System.out.println("====================");
}
private static int getIndex(int[] arr, int value) {
//A:定义最小索引,最大索引
int min = 0;
int max = arr.length-1;
//B:计算出中间索引
int mid = (min+max)/2;
//C:拿中间索引的值和要查找的元素进行比较
while(value!=arr[mid]){
//大了 在左边找
if(arr[mid]>value){
max = mid-1;
}else{
min = mid+1;
}
if(min>max){
return -1;
}
//E:回到B的位置。
mid = (min+max)/2;
}
return mid;
}
}
二分查找注意事项:
/*
* 注意:下面这种做法是错误的
* 因为数组本身时无序的,所以这种情况下的查找不能使用二分查找。(老老实实的用基本查找法)
* 所以你先排序了,但是你排序的时候已经改变了我原始的元素索引。
*/
public class 二分查找注意事项 {
public static void main(String[] args) {
int[] arr = { 24, 69, 80, 57, 13 };
bubbleSort(arr);
int index = getIndex(arr, 80);
System.out.println("index="+index);
}
//冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//二分查找
public static int getIndex(int[] arr, int value) {
// A:定义最小索引,最大索引
int min = 0;
int max = arr.length - 1;
// B:计算出中间索引
int mid = (min + max) / 2;
// C:拿中间索引的值和要查找的元素进行比较
while (value != arr[mid]) {
// 大了 在左边找
if (arr[mid] > value) {
max = mid - 1;
} else {
min = mid + 1;
}
if (min > max) {
return -1;
}
// E:回到B的位置。
mid = (min + max) / 2;
}
return mid;
}
}