您的当前位置:首页正文

Java基本类

2021-12-29 来源:步旅网


JAVA基本类库 1.Object类

Object类是Java程序中所有类的直接或间接父类,也是类库中所有类的父类,任何一个类都是由Object类派生出来的。所以它含有的属性和方法将被所有的类继承,下面就是Object类的方法,也是所有类都含有的方法:

1.1 protected Object clone( )throws CloneNotSupportedException

复制当前的对象,并返回这个复制的对象,该对象类型为Object。但所有需要使用该方法的类都必须实现接口cloneable,否则,运行时将抛出CloneNotSupportedException类的例外。

1.2 public final Class getClass( )

返回一个当前对象在运行期的Class类对象。

1.3 public int hashCode( )

返回一个hash code value,不同的对象有不同的hash code value.

1.4 public Boolean equals( Object obj )

如果当前对象与形参对象相同则返回true,否则返回false。

1.5 public String toString( )

返回一个反映这个对象信息的字符串。例如一个Eq类, Eq.toString() 输出Eq@c17164

1.6 public final void notify( )

这是关于多线程的方法。此方法用来唤醒等待监视器的多个线程中的一个。

1.7 public final void notifyAll( )

此方法用以唤醒所有等待监视器的线程

1.8 public final void wait( long timeout )throws InterrupedException

此方法是让当前线程放弃对这个对象的同步声明,即放弃对这个对象的锁定,进入等待行列,直到由notify()或notifyAll()方法唤醒,或形参中规定的时间到期,timeout的单位是毫秒。

1.9 public final void wait( long timeout,int nanos )throws InterrupedException

这个方法比上一个多了一个形参,第二个形参的意思是nanoseconds(十亿分之一秒),这个方法的等待时间变为两个形参所指示的时间的和,时间控制更精确。

1.10 public final void wait( )throws InterrupException

这个方法的含义同wait(0)

1.11 protected void finalize( )throws Throwable

这个方法用来把对象从内存中清除,由圾收集器自动调用。编程者可以重载这个方法,使得对象在被清除时,显示某些信息。

2. Class类

Class类是一个非常特殊的类,它的对象会存在于其他每个类。当一个类X被编译时,会由编译系统自动生成一个特殊的对象(Class对象)产生,并隐藏在X.class文件中。这个特殊的Class对象含有Class类的所有信息,且可以通过Class类的方法调用之,下面是Class类的一些方法:

2.1 public static Class forName(String className)throws ClassNotFoundException

此方法为类方法,可以用Class直接调用。本方法的参数是一个类名。它将返回一个形参所表示的类的Class对象。如:

class t=Class.forName(“java.lang.Thread”) // 产生Thread类的Class对象

2.2 public String getName( )

该方法返回Class对象代表的实体(类、接口、数组、基本数据类型等)的名字。例如: (new Object() ). getClass().getName() /* 输出java.lang.Object。 其中的getClass()用来得到当前对象的Class对象,同一个类的对象有相同的Class对象。*/

getName()的返回值以特殊的符号表示该实体的信息。‘[’表示数组,有几个‘[’表示几维数组。‘L’为类或接口。‘J’为long。‘Z’为boolean。其余均为大写首字母。例如:

(new double[3][2]).getClass().getName() // 输出 [[ D

2.3 public Class getSuperclass( )

此方法不同于Object类的方法getClass(),它返回的是一个数组,而数组元素是Class对象,这些对象是以当前类中的成员所对应的Class对象。

2.4 public ClassLoader getClassLoader( )

ClassLoader是个抽象类,在java.lang包中。任何一个类加载内存,都是通过对象来实现的,这个对象就是它衍生类的实例。因为类的定义都是以字节码文件形式存在,加载一个类就是读取这些字节码。

2.5 public Class getComponentType( )

返回数组元素的类型,如果当前对象不是数组,返回null。

2.6 public int getModifiers( )

返回类或接口的修饰符,但它们用一个十六进制数表示:public为0x0001,final为0x0010,abstract为0x0400„„ 这些数是JVM用来鉴别修饰符的。

2.7 public Class getDeclaringClass( )

如果当前对象是另一个类的成员,那么就返回那个类的Class对象,否则为空。

3. Math类

Math类是一个最终类。它包含了常用的科学计算方法。这些方法都是静态方法,可以通过类名直接调用。下面列出其中常用的常数和方法:

3.1 public static final double E

3.2 public static final double PI

三角函数:

3.3 public static double sin(double a)

3.4 public static double cos(double a)

3.5 public static double tan(double a)

3.6 public static double asin(double a)

3.7 public static double acos(double a)

3.8 public static double atan(double a)

弧度、角度转换函数:

3.9 public static double toRadians(double angdeg)

3.10 public static double toDegrees(double angrad)

代数函数:

3.11 public static double signum ( type a )

返回参数的符号函数:如果参数是零,则返回零;如果参数大于零,则返回 1.0;如果参数小于零,则返回 -1.0。type为double 或float 。

3.12 public static double exp( double a )

3.13 public static double cbrt( double a )

3.14 public static double pow ( double a, double b )

以上三者:exp可返回欧拉数e 的a次幂; cbrt可返回a 的立方; pow返回a的b次幂

3.15 public static double sqrt ( double a)

3.16 public static double hypot ( double x, double y)

22

以上两者: sqrt返回a的平方根;hypot 则返回sqrt(x +y)。

3.17 public static double log( double a )

3.18 public static double log10( double a )

以上两者:log返回a的自然对数 (即ln); log10返回a的常用对数(即lg)。

3.19 public static double ceil ( double a )

3.20 public static double floor ( double a )

以上两者:ceil返回大于等于a的最小整数;floor返回小于等于a的最大整数(高斯函数)

3.21 public static double rint ( double a )

3.22 public static long round(double a)

以上两者: 都是将a进行四舍五入。之后rint返回double类型;而round返回long类型。

3.23 public static double random( )

返回带正号的 double 值,大于或等于 0.0,小于 1.0。

以下方法均有各种数据类型的重载版本:

3.17 public static int abs(int a)

3.18 public static int max(int a,int b)

3.19 public static int min(int a,int b)

4.a String类

Java提供了两个用于字符串操作的类,一是String,另一个是StringBuffer。String类用于处理值不发生改变的字符串。StringBuffer类则用于那些可能发生变化的字符串的处理。由于String类对象都是常量,故它的处理效率要比StringBuffer高。

String的常用方法:

4.1a public int length( )

返回字符串长度

4.2a int hashCode()

返回此字符串的哈希码。

4.3a boolean isEmpty()

当且仅当 length() 为 0 时返回 true。

4.4a public char charAt( int index )

返回index位置的字符,index从0到length()-1 。

4.5a public int codePointAt ( int index )

public int codePointBefore ( int index )

以上二者:前者返回index处的字符,而后者返回index之前的字符(注:二者返回的都是int,如‘0’---48 )

4.6a public static String valueOf (type x )

valueOf函数是转化函数,可以将各种类型转化成String 。其中type类型有:boolean ;char ;double ;float ;int ;long ;char[] ;Object 。注意valueOf是类函数,调用很方便。

4.7a public char[] toCharArray( )

将此字符串转换为一个新的字符数组。

4.8a public void getChars ( int from, int to, char[ ] dst, int sta)

这个方法可以把字符串中的子串[from, to-1]复制到char数组dst中,从dst[sta]开始。

4.9a public byte[] getBytes ( String charsetName )

使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。

4.10a static String copyValueOf(char[] data)

static String copyValueOf(char[] data, int offset, int len) 使用字符数组构造string。第二个函数中:offset为字符数组中初始位置,len为复制的长度。

4.11a boolean equals ( Object anObject )

boolean equalsIgnoreCase (String anotherString) 比较此字符串与指定的对象。这是对Object类中同方法的重载。后者只能与另一个 String 比较且不考虑大小写。

4.12a boolean contentEquals ( StringBuffer str )

boolean contentEquals(CharSequence cs)

若当前字符串与StringBuffer、 CharSequence型字符串str相同,则返回 true。

4.13a public int compareTo( String str )

public int compare ToIgnoreCase ( String str )

以上二者均是字符串比较函数:如果当前字符串比字符串str靠前时(字典序),返回负数,相反时为正数。只是后者在比较时不考虑大小写。

4.14a public Boolean startsWith( String str )

public Boolean endsWith( String str )

上面两个方法分别判断字符串是否以str开始或结束。

4.15a public int indexOf( int ch )

public int indexOf( int ch, int fromIndex ) public int indexOf( String str )

public int indexOf( String str, int fromIndex ) 以上四个重载版本:均为搜索函数。搜索字符ch或子串str在此字符串中第一次出现的位置,如果没有找到,则返回-1。fromIndex表示搜索的起点,即从fromIndex开始进行搜索。

4.16a public int lastIndexOf( int ch )

public int lastIndexOf( int ch, int fromIndex ) public int lastIndexOf( String str )

public int lastIndexOf( String str, int fromIndex )

以上四个重载版本:均为反向搜索函数。使用方法与搜索函数完全相同。

4.17a public String substring( int beginIndex )

public String substring ( int beginIndex, int endIndex )

前者返回从当前串中由beginIndex开始到结尾形成的新字符串。后者则在endIndex-1处结束(与C++不同,不是长度!)。

4.18a public String concat ( String str )

把形参字符串str连接到当前字符串后。(字符串的加法运算)

4.19a public String replace ( char oldChar, char newChar )

把字符串中所有的字符oldChar换成newChar。

4.20a public String replaceAll ( String regex, String str )

public String replaceFirst ( String regex, String str ) 以上二者:前者使用字符串str 替换当前字符串中所有满足给定的正则表达式regex的子串;而后者则只替换第一个满足正则表达式的子串。

4.20a public String trim ( )

返回字符串的副本,并删除前导空白和尾部空白。

4.21a public String toLowerCase ( )

public String toUpperCase ( )

以上二者:返回字符串的副本,前者将字符都转换为小写;后者转换为大写。

4.22a boolean matches(String regex)

判断此字符串是否匹配给定的正则表达式。

4.23a boolean regionMatches(int toffset, String other, int ooffset, int len)

boolean regionMatches(boolean ignoreCase, int sta1, String other, int sta2, int len)

测试两个字符串区域是否相等。当前串从sta1开始,other串从sta2开始,长度为len。 ignoreCase = true 表示忽略大小写。

4.24a boolean contains(CharSequence s)

当且仅当此字符串包含指定的 char 值序列时,返回 true。

4.b StringBuffer类

构造函数:

StringBuffer() 构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。 StringBuffer(CharSequence seq) 使用CharSequence 构造一个StringBuffer StringBuffer(String str) 使用CharSequence 构造一个StringBuffer StringBuffer(int capacity) 指定初始化长度,但字符未初始化。

4.1b public int length ( ) 4.1 a

public char charAt ( int index ) 4.2 a public int codePointAt ( int index ) 4.3 a public int codePointBefore ( int index ) 4.4 a public int indexOf ( String str ) 4.15 a public int indexOf ( String str, int fromIndex ) 4.15 a public int lastIndexOf ( String str ) 4.16 a

public int lastIndexOf ( String str, int fromIndex ) 4.16 a public StringBuffer substring ( int start, int end ) 4.17 a public StringBuffer substring ( int start ) 4.18 a public void getChars(int from, int to, char[] dst, int sta)

以上均为StringBuffer与String一致的一些基本方法,不再详述。

4.2b public StringBuffer append ( type x )

将参数x转化成字符串后,连接到当前字符串的尾部,形成一个新的字符串,并返回之。 type包括简单类型:boolean ;char ;int ;double ;long ;float

还有复合类型:char[] ;CharSequence ;Object ;StringBuffer ;String 另外:char[] 和CharSequence 还有以下版本:

public StringBuffer append(char[] str, int start, int len)

public StringBuffer append(CharSequence s, int start, int end)

4.3b public StringBuffer insert ( int index, type x )

字符串插入函数。将参数x转化成字符串后,从index前进行插入。形成一个新的字符串,并返回之。

type包括简单类型:boolean ;char ;int ;double ;long ;float

还有复合类型:char[] ;CharSequence ;Object ;StringBuffer ;String 另外:char[] 和CharSequence 还有以下版本:

public StringBuffer insert(int index, char[] str, int start, int len)

public StringBuffer insert(int dstOffset, CharSequence s, int start, int end)

4.4b public StringBuffer deleteCharAt ( int index )

删除指定位置index处的字符。

4.5b public StringBuffer delete ( int start, int end )

删除start(含)到end(不含)之间的所有字符。( start=< x4.6b public void setCharAt ( int index, char ch )

将指定位置index处的字符换作ch(注意,不返回值)。

4.7b public StringBuffer replace ( int start, int end, String str )

从start(含)到end(不含)之间的子串以str替换。

4.8b public StringBuffer reverse( )

返回逆转字符串。

4.9b public int capacity( )

返回容量,通常会大于等于length()

4.10b public void setLength ( int newlength )

改变字符串长度,若newLength大于原长度,则新添的空字符‘\\0’。相反,字符串中最后的几个字符将被删除。形参newLength不能为负数。

4.11b public String toString( )

把StringBuffer型字符串转换成String型。

4.12b CharSequence subSequence(int start, int end) 返回CharSequence 包含当前串的字串[star , end-1]。

5.System类

系统类一个final类,所有的方法都用类变量调用,即不能实例化一个System类。System类主要提供了标准输入、输出以及一些系统环境信息。还有许多方法,可以管理Java虚拟机的运行和获得虚拟机的运行信息。

5.1 public static final InputStream in

标准输入

5.2 public static final PrintStream

标准输出

5.3 public static final PrintStream

标准错误输出

out er 6. 数据类型类

每一种简单数据类型都对应着一个数据类型类,例如,int对应着Integer类,double对应着Double类。某些场合只能使用这种数据类型类,而不能是简单的变量。而且使用这些类能完成简单变量不能完成的工作,如将一个数字字符串转化成一个整数等。

下面是Double Float Long Integer Short Byte 类所共有的一些常用函数。这些函数名字都相同。注:以下表示方法: type 表示上述任一种类型,classX 表示上述任一种类型对应的类,x表示上述任一种类型的一个参数。

6.1 MAX_VALUE & MIN_VALUE

常数,包含了classX的最大值和最小值。如 Byte. MAX_VALUE = 2-1

6.2 public classX ( type x ) public classX ( String str )

构造函数,分别把数字和数字字符串转成classX类。注:float还有一种构造方法: public Float( double x ) 6.3 public byte byteValue( )

public short shortValue( ) public int intValue( ) public long longValue( ) public float floatValue( ) public double doubleValue( )

类型转换函数:typeValue 将当前类classX 转化成各种相应的类型type 。其中由浮点数转换为整数时的规则源于浮点数至整数强制转换(即直接略去小数部分,没有四舍五入)。

6.4 public int hashCode( )

返回此classX 对象的哈希码。

6.5 public boolean equals( Object obj )

比较此classX对象与指定对象。

6.6 public int compareTo ( classX another )

比较两个classX 对象,若当前classX 对象大于another则返回1,相等是0,小于是 -1

6.7 public String toString ( )

public static String toString ( type x )

public static String toString ( type x , int radix ) 最后一个函数只用于整数型。 1函数将当前对象转换成字符串。如 123.5---> “123.5” 第二个函数将参数转换为字符串,如 Double.toString(3.14)3.14 。第三个函数中将进制是radix的数x转换成字符串。如:Integer.toString(123,10)---> “123”。注意:可以将数字x转换为进制在2-36之间的字符串。如果radix不在[2,36]范围,则自动变为10。各个数字使用0123456789abcdefghijklmnopqrstuvwxyz标示。

6.8 public static classX valueOf ( type x ) public static classX valueOf ( String s)

7

public static classX valueOf ( String s, int radix )

类函数。将数x或字符串str转换成classX类型。最后一个版本中radix 是基数。其使用范围与格式与praseInt相同。 其如:Integer.valueOf(“-100”,2) ---> -8

6.9 static int parseInt(String s)

static int parseInt(String s, int radix)

作用是将字符串转换为各种类型。有: parseByte、parseShort、parseLong、

parseFloat、parseDouble。第二个函数只作用于整数,其中radix为数制,必须在[2,36]的范围内。s中包含0-9和26个字母,其中大写与小写是等价的。如:(\"-a\ (\"-A\均表示-10。

仅double 与float共有的函数:

6.10 public static int compare ( type x , type y )

比较x与y的值,xy返回1 。

6.11 public static boolean isInfinite ( type x )

若x是无穷大,则返回 true,否则返回 false。 6.12 public boolean isNaN ( )

public static boolean isNaN ( type x )

若当前的classX值或指定的数字x是一个非数字 (NaN) 值,则返回 true,否则返回 false。

仅long 与int共有的函数:

6.13 public static int signum ( type x )

返回x的符号函数。正数是1,负数是-1,零是0 。

6.14 public static String toBinaryString ( type x ) public static String toOctalString ( type x ) public static String toHexString ( type x )

以上三者均是将x转化成字符串。第一个转换成二进制形式;第二个转换成八进制形式;第三个转换成十六进制形式。如:Integer.toBinaryString(8) -----> “100”

6.15 static int bitCount (int i)

返回指定 int 值的二进制补码表示形式的 1 位的数量。 static int reverse (int i)

返回通过反转指定 int 值的二进制补码表示形式中位的顺序而获得的值。 static int reverseBytes (int i)

返回通过反转指定 int 值的二进制补码表示形式中字节的顺序而获得的值。 static int rotateLeft (int i, int distance)

返回根据指定的位数循环左移指定的 int 值的二进制补码表示形式而得到的值。 static int rotateRight (int i, int distance)

返回根据指定的位数循环右移指定的 int 值的二进制补码表示形式而得到的值。

7. Character 类

Character类也属于数据类型类,但与以上6大类数据类型有不小的差别。但仍有很多有用的

函数,现将常用部分整理如下:

以下是Character类与6大类相同或类似的一些函数:

7.1 public Character ( char ch ) 6.2 public int hashCode ( ) 6.4 public boolean equals ( Object obj ) 6.5 public int compareTo( Character another ) 6.6 public String toString( ) 6.7 public static String toString( char ch ) 6.8 public static Character valueOf( char ch ) 6.9

以下是Character类独有的字符测试函数。它们都是类函数,可以用类名直接调用。并且均有两个重载版本:即type可以是char 或 int 。如 ‘0’或48。

7.2 public static boolean isDigit ( type x )

确定字符x是否为数字。

7.3 public static boolean isLetter ( type x )

确定字符是否为字母。

7.4 public static boolean isLowerCase ( type x )

确定字符x 是否为小写字母。

7.5 public static boolean isUpperCase ( type x )

确定字符是否为大写字母。

7.6 public static boolean isLetterOrDigit ( )

确定字符x是否为字母或数字。

7.7 public static boolean isWhitespace ( type x )

确定字符x是否为空白字符。

7.8 public static boolean isDefined ( type x )

确定字符x是否被定义为 Unicode 中的字符。

8. Math.BigInteger 类

字段: ZERO常量 0 | ONE常量 1 | TEN常量 10

构造方法:

8.1 BigInteger(byte[] val)

以二进制补码形式的将 byte 数组转换为 BigInteger。 BigInteger(int signum, byte[] magnitude)

以的符号-数量表示形式将byte数组转换为 BigInteger。 BigInteger(String val)

将 BigInteger 的十进制字符串表示形式转换为 BigInteger。

BigInteger(String val, int radix)

将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。 BigInteger(int bitLength, int certainty, Random rnd)

构造一个随机生成的正 BigInteger,它可能是一个具有指定 bitLength 的素数。 BigInteger(int numBits, Random rnd)

构造一个随机生成的 BigInteger,它是[ 0 , 2numBits – 1] 范围内均匀分布的值。

符号运算

8.2 int signum() 返回此 BigInteger 的正负号函数。-1 0 1

8.3 BigInteger negate() 返回其值是此 BigInteger的相反数。 8.4 BigInteger abs() 返回其值是此 BigInteger 的绝对值。

算数运算:

8.5 BigInteger add(BigInteger val) 返回其值为 (this + val) 的 BigInteger。 8.6 BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger。 8.7 BigInteger divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger。 8.8 BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger。

8.9 BigInteger remainder(BigInteger val) 返回其值为 (this % val) 的 BigInteger。 8.10 BigInteger mod(BigInteger m) 返回其值为 (this mod m) 的 BigInteger。

8.11 BigInteger[] divideAndRemainder(BigInteger val) 返回数组,形式为:{商,余数}。

8.12 BigInteger pow(int exponent) 返回其值为this 的 BigInteger。

exponent

8.13 BigInteger modPow(BigInteger exponent, BigInteger m) 返回 thismod m

8.14 BigInteger gcd(BigInteger val) 返回abs(this) 和 abs(val) 的最大公约数。

8.15 BigInteger modInverse(BigInteger m) 返回this模m的逆元。即:this*(this^-1)=1mod(m)。

注意:如果this模m没有逆元时,即this与m不互质时会抛出异常。

质数:

8.16 BigInteger nextProbablePrime() 返回大于this的可能为素数的第一个整数。(失误率 <2-100 ) 8.17 boolean isProbablePrime(int times) Miller法测质数,times为测试次数。 e.g: 2 --- true 8.18 static BigInteger probablePrime(int bitLength, Random rmd) 返回一个bitLength位的

质数,失误率<2-100,rmd = new Random();

exponent

数值比较:

8.19 BigInteger max(BigInteger val) 返回此 BigInteger 和 val 的最大值。 8.20 BigInteger min(BigInteger val) 返回此 BigInteger 和 val 的最小值。

8.21 int compareTo(BigInteger val) 将此 BigInteger < => val时,分别返回 -1 0 1。 8.22 boolean equals(Object x) 比较此 BigInteger 与指定的 Object 的相等性。

类型转换:

8.23 type typeValue() 将此 BigInteger 转换为 type类型,Type =double、float、long、int 8.24 byte[] toByteArray() 返回BigInteger 的二进制补码数组。

8.25 String toString() 返回此 BigInteger 的十进制字符串表示形式。

8.26 String toString(int radix) 返回此 BigInteger 的给定基数的字符串表示形式。2=8.27 static BigInteger valueOf(long val) 将long 转换为BigInteger。 8.28 int hashCode() 返回此 BigInteger 的哈希码。

位运算:

8.29 int bitCount() 返回此BigInteger二进制形式中与符号不同的位的数量。 8.30 int bitLength() 返回此 BigInteger二进制形式的位数,不包括符号位。

8.31 boolean testBit(int n) 指定位为1时,返回 true。

8.32 BigInteger setBit(int n) 返回设置了指定位的BigInteger副本。 8.33 BigInteger clearBit(int n) 返回清除了指定位的副本。 8.34 BigInteger flipBit(int n) 返回反转了指定位的副本。

8.35 BigInteger shiftLeft(int n) 返回其值为 (this << n) 的 BigInteger。 8.36 BigInteger shiftRight(int n) 返回其值为 (this >> n) 的 BigInteger。

注意:以上几函数中涉及到的指定位:二进制串的最右端为0,故1000中第3位=1,第0位=0

8.37 BigInteger and(BigInteger val) 返回其值为 (this & val) 的 BigInteger。 8.38 BigInteger andNot(BigInteger val) 返回其值为 (this & ~val) 的 BigInteger。 8.39 BigInteger not() 返回其值为 (~this) 的 BigInteger。

8.40 BigInteger or(BigInteger val) 返回其值为 (this | val) 的 BigInteger。 8.41 BigInteger xor(BigInteger val) 返回其值为 (this ^ val) 的 BigInteger。

9. Math.BigDecimal 类

字段:

ROUND_HALF_UP (通常使用的四舍五入的舍入模式) ZERO ONE TEN

构造函数:

9.1 BigDecimal(type val) 将 type转换为 BigDecimal。Type = int、long、double、BigInteger

BigDecimal(String val) 将 BigDecimal 的字符串表示形式转换为 BigDecimal。 BigDecimal(char[] in) 同上,只不过把string换作了char[]。 BigDecimal(char[] in, int offset, int len) 部分char数组

符号运算:

9.2 int signum() 返回此 BigDecimal 的正负号值。 9.3 BigDecimal abs() 返回BigDecimal 的绝对值 9.4 BigDecimal negate() 返回 BigDecimal的相反数

算数运算:

9.5 BigDecimal add(BigDecimal val) 返回 (this + val) 9.6 BigDecimal plus()

返回 BigDecimal,其值为 (+this),其标度为 this.scale()。 BigDecimal plus(MathContext mc)

返回其值为 (+this) 的 BigDecimal(根据上下文设置进行舍入)。

9.7 BigDecimal subtract(BigDecimal val)

返回一个 BigDecimal,其值为 (this - val).

BigDecimal subtract(BigDecimal subtrahend, MathContext mc)

返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。

9.8 BigDecimal multiply(BigDecimal val)

返回一个 BigDecimal,其值为 (this × val).

BigDecimal multiply(BigDecimal multiplicand, MathContext mc)

返回其值为 (this × multiplicand) 的 BigDecimal(根据上下文设置进行舍入)。

9.9 BigDecimal divide(BigDecimal divisor)

返回一个 BigDecimal,其值为 (this / divisor)

BigDecimal divide(BigDecimal divisor, int roundingMode)

返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。

BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。

BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。

BigDecimal divide(BigDecimal divisor, MathContext mc)

返回其值为 (this / divisor) 的 BigDecimal(根据上下文设置进行舍入)。

BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。

BigDecimal divideToIntegralValue(BigDecimal divisor)

返回 BigDecimal,其值为向下舍入所得商值 (this / divisor) 的整数部分。

BigDecimal remainder(BigDecimal divisor)

返回其值为 (this % divisor) 的 BigDecimal。

BigDecimal remainder(BigDecimal divisor, MathContext mc)

返回其值为 (this % divisor) 的 BigDecimal。(根据上下文设置进行舍入)。 BigDecimal[] divideAndRemainder(BigDecimal divisor)

返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟对两个操作数计算所得到的 remainder。

9.10 BigDecimal pow(int n)

返回其值为 (this^n) 的 BigDecimal,准确计算该幂,使其具有无限精度。

BigDecimal pow(int n, MathContext mc) 返回其值为 (this^n) 的 BigDecimal。

位运算:

9.11 BigDecimal movePointLeft(int n) 将该值的小数点向左移动 n 位。 9.12 BigDecimal movePointRight(int n) 将该值的小数点向右移动 n 位。

数值比较:

9.13 int compareTo(BigDecimal val) 将此 BigDecimal 与指定的 BigDecimal 比较。 9.14 boolean equals(Object x) 比较此 BigDecimal 与指定的 Object 的相等性。 9.15 BigDecimal max(BigDecimal val) 返回此 BigDecimal 和 val 的最大值。 9.16 BigDecimal min(BigDecimal val) 返回此 BigDecimal 和 val 的最小值。

类型转换:

9.17 Type typeValue() 将此 BigDecimal 转换为 type( int long double byte short )。

9.18 static BigDecimal valueOf(type val) 将type转换为BigDecimal。Type=long、double 9.19 String toString() 返回字符串表示形式,如果需要指数,则使用科学记数法。 9.20 String toPlainString() 返回不带指数字段的此 BigDecimal 的字符串表示形式。 9.21 String toEngineeringString() 返回字符串表示形式,需要指数时,则使用工程计数法。 9.22 BigInteger toBigInteger() 将此 BigDecimal 转换为 BigInteger。

9.23 static BigDecimal valueOf(double val) 9.24 static BigDecimal valueOf(long val)

将 double、long 值转换为具有零标度的 BigDecimal。

其他:

9.26 int hashCode() 返回此 BigDecimal 的哈希码。 9.27 int precision() 返回此 BigDecimal 的精度。

9.28 BigDecimal round(MathContext mc) 根据 MathContext 进行舍入后的 BigDecimal。 9.29 BigDecimal stripTrailingZeros() 移除当前BigDecimal所有尾部零后的 BigDecimal。

10. java.util.Arrays 类

此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。除非特别注明,否则如果指定数组引用为 null,则此类中的方法都会抛出 NullPointerException。

10.1 static int binarySearch(type[] a, type key)

static int binarySearch(type [] a, int fromIndex, int toIndex, type key) 二分搜索函数,在数组a中搜索key值。第二个函数指定了a[]中的搜索范围:

[fromIndex, toIndex) 。在调用此函数前应当排序,否则对a中多个相同元素而言无法确定找到的是哪个。返回! 如果找到目标值,则返回搜索键的索引;否则返回 “—(插入点- 1)”。插入点即第一个大于key的元素索引,如果数组中的所有元素都小于key,则为 a.length。注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。

以上两个函数支持各种类型的重载,type可以为:byte、char、double、int、long、object

static int binarySearch(T[] a, T key, Comparator c)

static int binarySearch(T[] a, int from, int to, T key, Comparator c)

使用二分搜索法来搜索指定数组,以获得指定对象。在进行此调用之前,必须根据指定的比较器(通过 sort(T[], Comparator )方法)对数组进行升序排序。如果没有对数组进行排序,则结果是不确定的。如果数组包含多个等于指定对象的元素,则无法保证找到的是哪一个。用于一般的类排序。

10.2 static type[] copyOf(type[] ary, int len)

复制指定的数组,截取len个元素,或用 0 填充(如有必要),以使副本具有指定的长度。 Type同样支持各种常见类型:byte、char、double、int、long、object

10.3 static type[] copyOfRange(type[] ary, int from, int to)

将指定数组的指定范围复制到一个新数组。From为要复制的范围的初始索引(包括)

to为要复制的范围的最后索引(不包括)。(此索引可以位于数组范围之外)。

10.4 static boolean equals(type[] a1, type[] a2)

判断传入的两个数组是否完全相等,是则返回true,反之为false。Type可以是多种类型。

10.5 static void fill(type[] a, type val)

static void fill(type[] a, int from, int to, type val)

相当于c++中的memset。将a[]数组填充为val值,第二个函数指定了填充复调范围为:[from, to-1]。

10.6 static int hashCode(type[] a)

传入数组a,返回a数组的哈希码。这个哈希码的产生基于指定数组的内容。如果a数组和b数组相等,则它们的哈希值必相等。 E.g:Arrays.hashCode(a)

10.7 static void sort(type[] a)

static void sort(type[] a, int from, int to)

对传入数组进行升序排序,第二个函数指定了排序范围:[from, to-1]。

static void sort(T[] a, Comparator c)

static void sort(T[] a, int from, int to, Comparator c)

根据指定比较器产生的顺序对指定对象数组进行排序。第二个函数指定了排序范围。

10.8 static String toString(type[] a)

返回指定数组内容的字符串表示形式。

附:Comparator 接口

public interface Comparator

int compare(T o1, T o2)函数,比较o1和o2,o1>o2返回1,=为0,<为-1.

例如要实现字符串排序,则代码如下:

class Cmp implements Comparator

{ public int compare(String s1, String s2){ return s1.compareTo(s2); }

}

String[] s = {“b”,”c”,”a”}; // in main cmp Cmp = new Cmp();

Arrays.sort(s,cmp); // 排序Over!

因篇幅问题不能全部显示,请点此查看更多更全内容