文章目录
一些注意点
- Java中BigInteger可以处理任意精度的整数计算,BigDecimal实现了任意精度的浮点数计算;
- BigInteger相关API
BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
int compareTo(BigInteger other)
static BigInteger valueOf(long x)//返回值为X的大整数
- BigDecimal相关API
BigDecimal add(BigDecimal other)
BigDecimal subtract(BigDecimal other)
BigDecimal multiply(BigDecimal other)
BigDecimal divide(BigDecimal other,RoundingMode mode)//RoundingMode代表舍入方式,RoundingMode.HALE_UP表示四舍五入
int compareTo(BigDecimal other)
static BigDecimal valueOf(long x)
static BigDecimal valueOf(long x,int scale)
//返回一个值为x或者值为x/(10的scale次方)的大浮点数
- 声明数组(数字数组初始化全为0,Boolean数组初始化全为false,对象数组初始化全为null)
int []a;//声明数组
int []a=new int[100]//声明并创建
- 数组一旦声明不可修改,因为内存为静态分配,
如果需要扩展数组大小,可以使用数组列表(array list)
- foreach循环使用方法
for(Type element:a){
//处理element
}
//如果想要打印数组的所有值,推荐使用Arrays.toString(a)
System.out.println(Arrays.toString(a));
- 数组初始化以及匿名数组
int []array={1,2,3,4};//初始化并创建,不需要指定长度
System.out.println(Arrays.toString(new int[] {1,2,3,4,5}));//创建匿名数组,不需要名字
-
Java中允许数组长度为0,
-
数组的拷贝
int []temp={1,2};
int []newarray=temp;//temp和newarray指向同一块地址,引用同一个数组
//如果希望数组将所有值复制一份,使用copyOf方法
int []copyarray=Arrays.copyOf(temp,temp.length);
//方法可以用来增进数组长度
- main方法中的args[]数组简单使用
在idea 中设置运行变量:
运行
System.out.println(Arrays.toString(args));
结果:
-
Math.random()返回[0,1)的随机浮点数
-
Java Arrays相关API
static String toString(type[] a)5.0
//返回包含a中元素的字符串,以逗号隔开
//a类型包括int long short char type boolean float double
static type copyOf(type[]a,int length)6
static type copyOfRange(type[]a,int start,int end)//6 返回一个与a类型的数组,长度为length或者end-start
static void sort(type[] a)//优化快排a
static int binarySearch(type[]a,type v)
static int binarySearch(type[]a,int start,int end,type v) //a有序
// 二分查找v,找不到返回负数值r, -r-1是a中v的有序位置
static void fill(type[]a,type v)//将数组所有元素设置为v
static boolean equals(type[]a,type[]b)//数组大小相同,且下标对应相等,则返回true
- 多维数组
double [][]a;
double [][]a=new double[max][max];
System.out.println(Arrays.deepToString(a))//快速打印a
- java中不规则数组
int [][]odds=new int[max][];//定义创建
for(int n=0;n<=3;n++){
odds[n]=new int[n+1];
}
结果为: