栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

7.Java基础-大数值以及数组

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

7.Java基础-大数值以及数组

文章目录
  • 一些注意点

一些注意点
  1. Java中BigInteger可以处理任意精度的整数计算,BigDecimal实现了任意精度的浮点数计算;
  2. 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的大整数
  1. 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次方)的大浮点数
  1. 声明数组(数字数组初始化全为0,Boolean数组初始化全为false,对象数组初始化全为null)
int []a;//声明数组
int []a=new int[100]//声明并创建
  1. 数组一旦声明不可修改,因为内存为静态分配,

如果需要扩展数组大小,可以使用数组列表(array list)

  1. foreach循环使用方法
for(Type element:a){
    //处理element
}
//如果想要打印数组的所有值,推荐使用Arrays.toString(a)
System.out.println(Arrays.toString(a));
  1. 数组初始化以及匿名数组
  int []array={1,2,3,4};//初始化并创建,不需要指定长度
        
	System.out.println(Arrays.toString(new int[] {1,2,3,4,5}));//创建匿名数组,不需要名字
  1. Java中允许数组长度为0,

  2. 数组的拷贝

int []temp={1,2};
int []newarray=temp;//temp和newarray指向同一块地址,引用同一个数组
        
 //如果希望数组将所有值复制一份,使用copyOf方法
int []copyarray=Arrays.copyOf(temp,temp.length);
//方法可以用来增进数组长度
  1. main方法中的args[]数组简单使用

在idea 中设置运行变量:

运行

        System.out.println(Arrays.toString(args));

结果:

  1. Math.random()返回[0,1)的随机浮点数

  2. 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
   
  1. 多维数组
double [][]a;
double [][]a=new double[max][max];

System.out.println(Arrays.deepToString(a))//快速打印a
    
  1. java中不规则数组
int [][]odds=new int[max][];//定义创建

for(int n=0;n<=3;n++){
    odds[n]=new int[n+1];
}

结果为:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/695027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号