Java Array类、大数据运算、包装类

Arrays类  工具类

数组引用为 null,空指针异常NullPointerException

import java.util.Arrays;

 

public class Demo02 {

public static void main(String[] args) {

int[] arr = {1,5,9,3,7,6}; //char[] arr={'z','A','r','a'}; 字符按码表排序

Arrays.sort(arr); // {1,3,5,6,7,9} 从小到大排序

 

String str = Arrays.toString(arr); // [1,3,5,6,7,9] 数组转字符串

 

int[] arr2={1,3,5,9,11,12}; 有序数组

int index5 = Arrays.binarySearch(arr2, 5)); //在数组的索引

int index4 = Arrasy.binarySearch(arr2, 4); //没有返回 负索引-1

System.out.println(index5);

System.out.println(index4);

}

}

 

System.arraycopy(src, 2, dest, 4, 3);  // src 索引2 3个 放desc 索引4

 

大数据运算

import java.math.BigDecimal; // 避免损失精度

import java.math.BigInteger; // 超过long的数据进行运算

public class Demo03 {

public static void main(String[] args) {

BigInteger bin1=new BigInteger("111111111111111111111111111");

BigInteger bin2=new BigInteger("999999999999999999999999999");

BigInteger binadd=bin1.add(bin2) //

System.out.println(binadd);

System.out.println(bin1.subtract(bin2)); //

System.out.println(bin1.multiply(bin2)); //

System.out.println(bin2.divide(bin1)); //

 

System.out.println(0.09 + 0.01); //0.09999999999999999

System.out.println(1.301 / 100); //0.01300999999999999

BigDecimal bd1 = new BigDecimal("0.09"); // 避免损失精度

BigDecimal bd2 = new BigDecimal("0.01");

BigDecimal bdAdd = bd1.add(bd2); //subtract/multiply/divide

System.out.println(bdAdd); // 0.10

 

BigDecimal bd3 = new BigDecimal("1.301");

BigDecimal bd4 = new BigDecimal("100");

System.out.println(bd3.divide(bd4,2,BigDecimal.ROUND_CEILING));

//除数  小数位数 向上取整  

}

}

 

包装类  

int类型与Integer对象的转换  其他基本类型转换方式相同。

import java.util.ArrayList;

public class Demo04 {

public static void main(String[] args) {

// 基本类型 转 包装类

Integer in=new Integer(12);  // 装箱

Integer in2=new Integer("123");

 

Integer in3=Integer.valueOf(12);

Integer in4=Integer.valueOf("123");

 

// 包装类 转 基本类型

int i=in.intValue(); // 拆箱

 

jdk1.5加了自动装箱

自动拆箱:对象自动直接转成基本数值

自动装箱:基本数值自动直接转成对象

 

Integer in=1; //自动装箱 Integer in=new Integer(1);

int sum=in+2; //自动拆箱 int sum=in.intValue()+2;

System.out.println(sum); // 3

 

ArrayList<Integer> arr=new ArrayList<Integer>();

arr.add(1); //自动装箱,list.add(new Integer(1));

 

Integer n1=500; // null 空指针异常

Integer n2=500;

System.out.println(n1==n2); //false  对象 比地址

System.out.println(n1.equals(n2));// true  内容

 

Integer a = new Integer(50); // 格式异常

Integer b = new Integer(50);

System.out.println(a==b); //false  对象

System.out.println(a.equals(b)); //true  内容

 

Integer c=50; // 50值 的地址  byte范围的在常量池中

Integer d=50; // 50值 的地址

System.out.println(c==d); // true  对象 比地址  

System.out.println(c.equals(d)); // true  内容

}

}

 

posted @ 2019-01-04 11:04  博客张C  阅读(395)  评论(0编辑  收藏  举报