常用API
Math类:
方法名 | 说明 |
---|---|
public static int abs(int a) | 返回参数的绝对值 |
public static double ceil(double a) | 向上取整 |
public static double floor(double a) | 向下取整 |
public static int round(float a) | 四舍五入 |
public static int max(int a,int b) | 返回两个int值中的较大值 |
public static int min(int a,int b) | 返回两个int值中的较小值 |
public static double pow(double a,double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的正值,[0.0,1.0) |
public class Test01 {
public static void main(String[] args) {
//3的5次幂
double pow = Math.pow(3, 5);
System.out.println(pow);
//3.2向上取整
double ceil = Math.ceil(3.2);
System.out.println(ceil);
//3.8向下取整
double floor = Math.floor(3.8);
System.out.println(floor);
//5.6四舍五入
long round = Math.round(5.6);
System.out.println(round);
}
}
System类:
方法名 | 说明 |
---|---|
public static void exit(int status) | 终止当前运行的Java虚拟机,非零表示异常终止 |
public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数) | 数组copy |
//arraycopy
import static java.lang.System.arraycopy;
public class Test01 {
public static void main(String[] args) {
int[] arr = {
10,27,8,5,2,1,3,55,88};
int[] newArr = new int[5];
arraycopy(arr,2,newArr,0,5);
for (int i = 0; i < newArr.length; i++) {
System.out.println(newArr[i]);
}
}
}
//返回当前时间(以毫秒为单位)
long start = System.currentTimeMillis();
Object类:
每个类都可以将Object作为父类,所有的类都直接或间接的继承自该类
构造方法:public Object()
方法名 | 说明 |
---|---|
public String toString() | 返回对象的字符串表示形式.建议所有的子类重写该方法,自动生成,打印起来看着信息方便 |
public boolean equals(另一个对象) | 比较对象是否相等.默认比较地址,重写可以比较内容,自动生成 |
Objects类:
方法名 | 说明 |
---|---|
public static String toString(对象) | 返回参数中对象的字符串表示形式 |
public static String toString(对象,默认字符串) | 返回对象的字符串表示形式 |
public static Boolean isNull(对象) | 判断对象是否为空 |
public static Boolean nonNull(对象) | 判断对象是否不为空 |
BigDecimal类:
计算机中的double不精确,比较普通的可以用double.如果要求运算时,要求精度,千万不能使用double,用BigDecimal用来精确计算
创建BigDecimal的对象,构造方法使用参数类型为字符串的
四则运算中的除法,如果除不尽,使用divide的三个参数的方法.
方法名 | 说明 |
---|---|
BigDecimal(double val) | 参数为double |
BigDecimal(String val) | 参数为string |
public BigDecimal add(另一个BigDecimal对象) | 加法 |
public BigDecimal subtract(另一个BigDecimal对象) | 减法 |
public BigDecimal multiply(另一个BigDecimal对象) | 乘法 |
public BigDecimal divide(另一个BigDecimal对象) | 除法 |
public BigDecimal divide(另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
舍入模式:1.进一法 BigDecimal.ROUND_UP
2.去尾法 BigDecimal.ROUND_FLOOR
3.四舍五入BigDecimal.ROUND_HALF_UP
比long类型更大的整数存的时候 BigInteger
public class Test01 {
public static void main(String[] args) {
double[] arr = {
0.1,0.2,2.1,3.2,5.56,7.21};
BigDecimal sum = new BigDecimal("0.0");
for (int i = 0; i < arr.length; i++) {
BigDecimal bd = BigDecimal.valueOf(arr[i]);
sum = sum.add(bd);
}
BigDecimal avg = sum.divide(BigDecimal.valueOf(arr.length),2, BigDecimal.ROUND_HALF_UP);
sum = sum.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("数组的平均值为:"+avg);
System.out.println("数组的和为:"+sum);
}
}
包装类:
泛型—>只能使用引用数据类型
一般用来把"字符串类型数据"转换为"基本数据类型"
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer类:该对象中包装了一个基本数据类型Int
方法名 | 说明 |
---|---|
public Integer(int value) | 根据int创建Integer对象(过时) |
public Integer(String s) | 根据string值创建Integer对象(过时) |
public static Integer valueOf(int i) | 返回表示指定的int值的integer实例 |
public static Integer valueOf(String s) | 返回一个保存指定值的integer对象string |
自动装箱和拆箱:
自动装箱:把基本数据类型转换为对应的包装类型
拆箱:把包装类类型----->对应的基本数据类型
static int parseInt(string s) 将字符串类型的整数变成int类型的整数
public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个分数:");
String fen = sc.next();
int f = parseInt(fen);
System.out.println(f+5);
}
}
string转基本数据类型:
**static int parseInt(string s)
int 转为string
1.字符串+任意数据类型都会变成字符串
2.调用string类中的valueOf方法
例子:
public class Test {
public static void main(String[] args) {
//字符串转换为基本数据类型
//int
String s1 = "100";
int i = Integer.parseInt(s1);
//double
String s2 = "10.2";
double v = Double.parseDouble(s2);
//boolean
String s3 = "true";
boolean b = Boolean.parseBoolean(s3);
//字符
String s4 = "a";
char c = s4.charAt(0);
//基本数据类型转字符串
//1.
int a = 100;
String n1 = a + "";
System.out.println(n1 + 100);
//2.
String a2 = String.valueOf(a);
System.out.println(a2 + 100);
}
}
将一个字符串里面的数字转换成int类型的数组
public class MyInteger {
public static void main(String[] args) {
String s = "91 27 46 38 50";
String[] strArr = s.split(" ");
int[] intArr = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) {
//将字符串类型的整数变成int类型的整数
int number = Integer.parseInt(strArr[i]);
intArr[i] = number;
}
for (int i = 0; i < intArr.length; i++) {
System.out.println(intArr[i]);
}
}
}
数组的高级操作-------二分查找:
每次去掉一半的查找范围
public class ErFen {
public static void main(String[] args) {
//二分查找
int[] arr = {
1,2,3,4,5,6,7,8,9};
int number = 3;
//1
int index = binarySearchForIndex(arr,number);
System.out.println(index);
}
private static int binarySearchForIndex(int[] arr, int number) {
int min = 0;
int max = arr.length-1;
while(min <= max){
int mid = (min + max) >> 1;
if(arr[mid] > number){
max = mid -1;
}else if(arr[mid] < number){
min = mid + 1;
}else{
return mid;
}
}
return -1;
}
}
数组的高级操作-------冒泡排序:
递归:
方法定义中调用方法本身
//兔子
public class Rabbit {
public static void main(String[] args) {
int a =getNum(7);
System.out.println(a);
}
public static int getNum(int n) {
if(n ==1 || n ==2){
return 1;
}else{
return getNum(n-1) + getNum(n-2);
}
}
}
Arrays类:
方法名 | 说明 |
---|---|
public static string tostring(int[] a) | 返回指定数组的内容的字符串表示形式 |
public static string sort(int[] a) | 按照数字顺序排列指定的数组 |
public static int binarySearch(int[] a) | 利用二分查找返回指定元素的索引 |
数组排序 找索引
import java.util.Arrays;
public class Test02 {
public static void main(String[] args) {
int[] arr = {
431,54,25,25,26,45,2,4,65,3,64,6,46,7,54};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
int index1= Arrays.binarySearch(arr,2);
int index2= Arrays.binarySearch(arr,200);
System.out.println(index1);
System.out.println(index2);
}
}
//返回指定数组的内容的字符串表示形式
int[] arr = {
1,2,432,32,54,32,3,7,657,563,25,43,6,463,52};
String a = Arrays.toString(arr);
System.out.println(a);