java程序员从笨鸟到菜鸟之(十八)常用类Arrays和BigDecimal
来源:互联网 发布:2006的网络男歌手 编辑:程序博客网 时间:2024/04/29 07:10
Arrays
Arrays:此类包含用来操作数组(比如排序和搜索)的各种方法(针对数组操作的工具类)
常用的几个成员方法:
1--public static String toString(int[] a)---->将任意类型的数组以字符串形式显示出来!
2--public static void sort(int[] a)---->将给定数组中元素升序排序-----快速排序
3--public static int binarySearch(int[] a, int key)---->当前int数组一定是有序数组----二分搜索法(折半查找)
4--Arrays.asList() ---->将数组转型为List集合
5--Arrays.copyof() ---->进行CopyOf的数组复制:比 System.arraycopy()的方法更加灵活
注意:二分法查不到时返还的是-(low+1)---->通过源码可以看出
实例1:
package org.westos.array_02;import java.util.Arrays;public class ArraysDemo {public static void main(String[] args) {//定义一个数组int[] arr = {24,69,80,57,13} ;//讲数组转成字符串的形式System.out.println("toString():"+Arrays.toString(arr));//[24, 69, 80, 57, 13]----内部StringBulider的append,l另外添了"["和"]"//public static void sort(int[] a):快速排序:(将给定数组中元素升序排序)Arrays.sort(arr) ;//在调用Arrays.toString(int[] a):将排好序的数组以String形式显示出来System.out.println(Arrays.toString(arr) );//[13, 24, 57, 69, 80]//public static int binarySearch(int[] a, int key)//查找57元素在数组中的索引int index = Arrays.binarySearch(arr, 57) ;System.out.println("index:"+index);//需求:查找577这个元素的索引int index2 = Arrays.binarySearch(arr, 577) ;System.out.println("index2:"+index2);//-6}}练习1:
package org.westos.array_02;import java.util.Scanner;/** * 字符串中的字符进行排序。 举例:"dacgebf" 结果:"abcdefg" 改进:键盘录入一个字符串 * @author */public class ArrayTest {public static void main(String[] args) {//创建键录入对象Scanner sc = new Scanner(System.in) ;//录入并接收数据System.out.println("请你输入一个字符串:");String line = sc.nextLine();//将"字符串"转换"字符数组"(很重要!!!!!!)----疑问?(字符串必须是字符形式的字符串吗?)char[] chs = line.toCharArray() ;//给字符数组进行冒泡排序(从小到大顺序)bubbleSort(chs) ;//最终要的是字符串,还需要将字符数组转换成字符串String result = String.valueOf(chs) ;//很重要System.out.println("result:"+result);}//冒泡排序(自己写的静态方法)public static void bubbleSort(char[] chs){for(int x = 0 ; x < chs.length-1 ; x ++){for(int y = 0 ; y <chs.length-1-x ; y++){if(chs[y]>chs[y+1]){//中间变量互换char temp = chs[y] ;chs[y] = chs[y+1] ;chs[y+1] = temp ;}}}}}对toString源码说明:
/* public static String toString(int[] a): Arrays的toString(int[] arr)的源码Java面向对象的原则: 实际开发中有对象存在,一定要给对象做非空判断! //对此数组的说明:arr=a={24,69,80,57,13} public static String toString(int[] a) { if (a == null) //判断数组对象为空 return "null"; //当前对数组对象为空 int iMax = a.length - 1;//数组的最大索引 if (iMax == -1) //数组对象不为空,但是没有数据 return "[]"; //此时返回[]//数据对象不为空,并且有数据:24, 69,80,57,13 StringBuilder b = new StringBuilder();//创建一个字符串缓冲区 b.append('['); //拼接了左中括号 for (int i = 0; ; i++) { b.append(a[i]); //字符串缓冲区对象.append(arr[i]) if (i == iMax)//i==4 return b.append(']').toString(); //13] b.append(", "); //没有取到最大索引:24, 69, 80, 57, } } //排序后的:[13, 24, 57, 69, 80] */
BigDecimal
Java在java.math包中提供的API中BigDecimal类,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象;构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象
引用链接:点击打开链接
补充:对于浮点类型的数据类说,它们的存储方式和整数的存储是不一致的,是按照有效数字位来进行存储的,浮点类型的数据计算的时候容易损失精度,计算出来的结果不精确,Java针对这种情况,提供了这个此类
BigDecimal:作用→来提供浮点类型数据的精确计算!可变的、任意精度的有符号十进制数
常用的构造方式:
public BigDecimal(int val)--------(将 int 转换为 BigDecimal将 int 转换为 BigDecimal----装箱)
public BigDecimal(String val)---(补充:String必须是数字字符串)
实例2:不精确(double类型)
public class BigDecimalDemo {public static void main(String[] args) {System.out.println(1.0-0.32);System.out.println(0.01+0.09);System.out.println(1.501*100);System.out.println(1.031/100);}}
编译结果:(回头贴图)
BigDecimal常用的成员方法:
1---public BigDecimal add(BigDecimal augend):加
2---public BigDecimal subtract(BigDecimal subtrahend):减
3---public BigDecimal multiply(BigDecimal multiplicand):乘法
4---public BigDecimal divide(BigDecimal divisor):除
5---public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
对最后一个方法的说明--------参数1:商;参数2:保留几位小数;参数3:舍入的一种模式(常用ROUND_HALF_UP)
实例3
package org.westos.bigdecimal_01;import java.math.BigDecimal;/** * public BigDecimal(String val) * 常用的成员方法; * public BigDecimal add(BigDecimal augend):加 * public BigDecimal subtract(BigDecimal subtrahend):减 * public BigDecimal multiply(BigDecimal multiplicand):乘法 * public BigDecimal divide(BigDecimal divisor):除 * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode) * 参数1:商,参数2:保留几位小数,参数3:舍入的一种模式:ROUND_HALF_UP * @author Orange */public class BigDecimalDemo2 {public static void main(String[] args) {//创建BigDecimal对象BigDecimal bd1 = new BigDecimal("0.01") ;BigDecimal bd2 = new BigDecimal("0.09") ;/* * 1:public BigDecimal add(BigDecimal augend) * 功能:加---→注意语法 * 返回值类型:BigDecimal * */System.out.println("add:"+bd1.add(bd2));//说明:bd1、bd2本身不变System.out.println(bd1);System.out.println(bd2);BigDecimal bd3 = new BigDecimal("1.0") ;BigDecimal bd4 = new BigDecimal("0.32") ;/* * 2:public BigDecimal subtract(BigDecimal subtrahend) * 功能:减---→注意语法 * * */System.out.println("sub:"+bd3.subtract(bd4));//0.68/* * 3:public BigDecimal multiply(BigDecimal multiplicand) * 功能:乘法---→注意语法 * 返回值类型 * */BigDecimal bd5 = new BigDecimal("1.501") ;BigDecimal bd6 = new BigDecimal("100.0") ;System.out.println("mul:"+bd5.multiply(bd6));/* * 4:public BigDecimal divide(BigDecimal divisor) * 功能:除 * 返回值类型:BigDecimal * 参数类型:BigDecimal * */BigDecimal bd7 = new BigDecimal("1.301") ;BigDecimal bd8 = new BigDecimal("100") ;System.out.println("div:"+bd7.divide(bd8));/* * 5:public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode) * BigDecimal divisor----参数1:商 * int scale-------------参数2:保留几位小数 * int roundingMode------参数3:舍入的一种模式:ROUND_HALF_UP * */System.out.println("div:"+bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));System.out.println("div:"+bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));}}包装类型补充:
BigInteger:用来计算超出了Integer类型范围的数据
实例4
package org.westos.biginteger;import java.math.BigInteger;/** * * BigInteger:用来计算超出了Integer类型范围的数据 * @author Apple */public class BigIntegerDemo {public static void main(String[] args) {/* 1: * 查看Integer类的最大值(范围) * 通过:MAX_VALUE这个静态常量值 * */System.out.println(Integer.MAX_VALUE);//2147483647/* 2: * 既然BigInteger是用来计算超出了Integer类型范围的数据 * 那么我们来创建Integer对象,看看是否报错 * * */Integer i = new Integer("2147483648") ;System.out.println(i);/* 3: * 当前该数据已经超出了Integer的取值范围 * 报错:java.lang.NumberFormatException: For input string: "2147483648" * 翻译:数字格式化异常 * *//* * 4:解决方法 * */BigInteger j = new BigInteger("2147483648") ;System.out.println("i:"+j);}}BigInteger的构造方法:
public BigInteger(String val)----将字符串(数字字符串)封装成BigInteger类型
成员方法:
1---public BigInteger add(BigInteger val)----------加
2---public BigInteger subtract(BigInteger val)----减
3---public BigInteger multiply(BigInteger val)-----乘
4---public BigInteger divide(BigInteger val)-------除
5---public BigInteger[] divideAndRemainder(BigInteger val)——除(特殊)
最后一个说明:返回一个BigInteger数组,数组中的元素---商(索引为0)、余数(索引为0)
实例5:
package org.westos.biginteger;import java.math.BigInteger;public class BigIntegerDemo2 {public static void main(String[] args) {//创建BigInteger对象BigInteger bg1 = new BigInteger("100") ;BigInteger bg2 = new BigInteger("50") ;/* * 1:public BigInteger add(BigInteger val) * 返回值类型:BigInteger * 参数类型: BigInteger * */System.out.println("add:"+bg1.add(bg2)); /* * 2:public BigInteger subtract(BigInteger val) * 返回值类型:BigInteger * 参数类型:BigInteger * */System.out.println("sub:"+bg1.subtract(bg2));/* * 3:public BigInteger multiply(BigInteger val) * 返回值类型和参数类型 * */System.out.println("mul:"+bg1.multiply(bg2));/* * 4:public BigInteger divide(BigInteger val) * 返回值类型和参数类型 * */System.out.println("div:"+bg1.divide(bg2));/* * 5:public BigInteger[] divideAndRemainder(BigInteger val) * 返回值类型:BigInteger[]---BigInteger数组类型 * 数组只有两个元素,索引0(熵),索引1(余数) * */BigInteger[] datas = bg1.divideAndRemainder(bg2) ;System.out.println("datas[0]:"+datas[0]);//2System.out.println("datas[1]:"+datas[1]);//0}}
常见的排序方法-----面试中用得到,必须掌握这几种方法的思想以及代码编写(用c++,c,java编写)
(1)冒泡排序
(2)选择排序
(3)插入排序
(4)快速排序
(5)折半查找(二分法)
Arrays相关链接:点击打开链接,点击打开链接
其它TCP/IP协议:点击打开链接
- java程序员从笨鸟到菜鸟之(十八)常用类Arrays和BigDecimal
- java程序员从笨鸟到菜鸟之(二十八)集合TreeSet排序之compareTo和compare方法
- java程序员从笨鸟到菜鸟之(十九)常用类Calendar和Date
- java程序员从笨鸟到菜鸟之(二十)常用类Math和System
- Java程序员从笨鸟到菜鸟之(七十六)细谈Hibernate(十八)悲观锁和乐观锁解决hibernate并发
- Java程序员从笨鸟到菜鸟之(二十八)Javascript总结之语言基础
- Java程序员从笨鸟到菜鸟之(二十八)Javascript总结之语言基础
- Java程序员从笨鸟到菜鸟之(二十八)Javascript总结之语言基础
- Java程序员从笨鸟到菜鸟之(二十八)Javascript总结之语言基础
- Java程序员从笨鸟到菜鸟之(十八)JSP基本语法与动作指令
- Java程序员从笨鸟到菜鸟之(十八)JSP基本语法与动作指令
- Java程序员从笨鸟到菜鸟之(十八)JSP基本语法与动作指令
- Java程序员从笨鸟到菜鸟之(十八)JSP基本语法与动作指令
- Java程序员从笨鸟到菜鸟之(十八)JSP基本语法与动作指令
- java程序员从笨鸟到菜鸟之(十一)常用类中的String
- java程序员从笨鸟到菜鸟之(十一)Object类
- java程序员从笨鸟到菜鸟之(十三)Scanner类
- java程序员从笨鸟到菜鸟之(十四)StringBuffer类
- windows下安装linux双系统绕坑指南
- 关于写这个博客的原因(一些心路历程)
- Linux单元总结(15)
- python最内嵌套作用域规则
- Java学习——关系运算符
- java程序员从笨鸟到菜鸟之(十八)常用类Arrays和BigDecimal
- [学习随笔]搭建Struts2开发环境
- 代理写错位置导致代理方法不能执行
- 深搜和广搜 迷宫最短路径
- 车牌字符的切割(一)
- Struts2 常量(SUCCESS、NONE、ERROR、INPUT、LOGIN)_详解
- junit junit suite的简单使用总结(从无到有)
- centos 7 通过yum安装dnf 包管理器
- 递归和非递归方法求斐波那契数