5.控制流程和数组、大数值

目录

1.控制流程

1.1 条件语句

1.2 switch case 语句

1.3 for 循环结构

1.4 增强 for 循环

1.5 while 循环结构

1.6 do...while循环结构

1.7 break 关键字

1.8 continue 关键字

2.数组

2.1 声明数组变量

2.2 创建数组

2.3 处理数组

2.4 多维数组的动态初始

2.5 java.util.Arrays

3.大数值

3.1 java.math.BigDecimal

3.2 java.math.BigInteger 


1.控制流程

1.1 条件语句

一个 if 语句包含一个布尔表达式和一条或多条语句。

语法1:

if(布尔表达式) { 

    //如果布尔表达式为true将执行的语句 

}

如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代码。

语法2:

if(布尔表达式){

    //如果布尔表达式的值为true 

}else{ 

    //如果布尔表达式的值为false

}

语法3:

if(布尔表达式 1){

    //如果布尔表达式 1的值为true执行代码 

}else if(布尔表达式 2){

    //如果布尔表达式 2的值为true执行代码 

}else if(布尔表达式 3){ 

    //如果布尔表达式 3的值为true执行代码

}else {

    //如果以上布尔表达式都不为true执行代码

}

if 语句后面可以跟 elseif…else 语句,这种语句可以检测到多种可能的情况。

使用 if,else if,else 语句的时候,需要注意下面几点:

  • if 语句至多有 1 个 else 语句,else 语句在所有的 elseif 语句之后。
  • if 语句可以有若干个 elseif 语句,它们必须在 else 语句之前。
  • 一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。

语法4

if(布尔表达式 1){ 

    //如果布尔表达式 1的值为true执行代码 

    if(布尔表达式 2){

        如果布尔表达式 2的值为true执行代码

    }

}

1.2 switch case 语句

语法:

switch(expression){
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}

switch case 语句有如下规则:

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。

  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。

  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

1.3 for 循环结构

for循环执行的次数是在执行前就确定的。

语法格式如下:

for(初始化; 布尔表达式; 更新) {
    //代码语句
}

关于 for 循环有以下几点说明:

  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
  • 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
  • 执行一次循环后,更新循环控制变量。
  • 再次检测布尔表达式。循环执行上面的过程。

1.4 增强 for 循环

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

语法格式

for(声明语句 : 表达式)
{
   //代码句子
}

1.5 while 循环结构

只要布尔表达式为 true,循环就会一直执行下去。

语法

while( 布尔表达式 ) {
  //循环内容
}

1.6 do...while循环结构

对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。

do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。

语法

do {
       //代码语句
}while(布尔表达式);

1.7 break 关键字

break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。

break 跳出最里层的循环,并且继续执行该循环下面的语句。

语法

break 的用法很简单,就是循环结构中的一条语句:

break;

//或者 

while(布尔表达式){
  if(布尔表达式) break;
}

//或者

flag1:  
for(){
 for(){
  if(布尔表达式) break flag1;
      }
}
//break flag1 之后执行这里
if(布尔表达式){
 //code
}

1.8 continue 关键字

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到更新语句。

在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

语法

continue 就是循环体中一条简单的语句:

continue;

//或者 

while(布尔表达式){
  if(布尔表达式) continue;
}

//或者

flag1:  
for(){
 for(){
  if(布尔表达式) continue flag1;
      }
}
//continue flag1 之后不会执行这里,而是继续从外层for继续执行
if(布尔表达式){
 //code
}

2.数组

2.1 声明数组变量

dataType[] arrayRefVar;   // 首选的方法
 
//或
 
dataType arrayRefVar[];  // 效果相同,但不是首选方法

2.2 创建数组

arrayRefVar = new dataType[arraySize];

上面的语法语句做了两件事:

  • 一、使用 dataType[arraySize] 创建了一个数组。
  • 二、把新创建的数组的引用赋值给变量 arrayRefVar。

数组变量的声明,和创建数组可以用一条语句完成,如下所示:

dataType[] arrayRefVar = new dataType[arraySize];

//或 

dataType[] arrayRefVar = {value0, value1, ..., valuek};

2.3 处理数组

数组的元素类型和数组的大小都是确定的,所以当处理数组元素时候,我们通常使用基本循环或者 foreach 循环。

数组可以作为入参和出参。

2.4 多维数组的动态初始

type arrayName = new type[arraylenght1][arraylenght2];

//或者

String s[][] = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");

2.5 java.util.Arrays

  • 该类包含用于操作数组的各种方法(如排序和搜索)。 该类还包含一个静态工厂,可以将数组视为列表。

Modifier and TypeMethod and Description
static <T> List<T>asList(T... a)

返回由指定数组支持的固定大小的列表。

static intbinarySearch(byte[] a, byte key)

使用二进制搜索算法搜索指定值的指定字节数组。

static intbinarySearch(byte[] a, int fromIndex, int toIndex, byte key)

使用二进制搜索算法搜索指定值的指定字节数组的范围。

static intbinarySearch(char[] a, char key)

使用二进制搜索算法搜索指定数组的指定值。

static intbinarySearch(char[] a, int fromIndex, int toIndex, char key)

使用二分搜索算法搜索指定值的指定数组的范围。

static intbinarySearch(double[] a, double key)

使用二进制搜索算法搜索指定值的指定数组的双精度值。

static intbinarySearch(double[] a, int fromIndex, int toIndex, double key)

使用二分搜索算法搜索指定值的指定数组的双精度范围。

static intbinarySearch(float[] a, float key)

使用二叉搜索算法搜索指定数组的浮点数。

static intbinarySearch(float[] a, int fromIndex, int toIndex, float key)

使用二分搜索算法搜索指定数组的浮点数范围。

static intbinarySearch(int[] a, int key)

使用二叉搜索算法搜索指定的int数组的指定值。

static intbinarySearch(int[] a, int fromIndex, int toIndex, int key)

使用二叉搜索算法搜索指定值的指定数组的范围。

static intbinarySearch(long[] a, int fromIndex, int toIndex, long key)

使用二分搜索算法搜索指定值的指定数组的范围。

static intbinarySearch(long[] a, long key)

使用二进制搜索算法搜索指定数组的指定数组。

static intbinarySearch(Object[] a, int fromIndex, int toIndex, Object key)

使用二进制搜索算法搜索指定对象的指定数组的范围。

static intbinarySearch(Object[] a, Object key)

使用二叉搜索算法搜索指定对象的指定数组。

static intbinarySearch(short[] a, int fromIndex, int toIndex, short key)

使用二进制搜索算法搜索指定值的指定数组的短整型范围。

static intbinarySearch(short[] a, short key)

使用二进制搜索算法搜索指定值的指定数组的指定值。

static <T> intbinarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)

使用二进制搜索算法搜索指定对象的指定数组的范围。

static <T> intbinarySearch(T[] a, T key, Comparator<? super T> c)

使用二叉搜索算法搜索指定对象的指定数组。

static boolean[]copyOf(boolean[] original, int newLength)

使用 false (如有必要)复制指定的数组,截断或填充,以使副本具有指定的长度。

static byte[]copyOf(byte[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static char[]copyOf(char[] original, int newLength)

复制指定的数组,截断或填充空字符(如有必要),以便复制具有指定的长度。

static double[]copyOf(double[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static float[]copyOf(float[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static int[]copyOf(int[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static long[]copyOf(long[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static short[]copyOf(short[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static <T> T[]copyOf(T[] original, int newLength)

复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。

static <T,U> T[]copyOf(U[] original, int newLength, <? extends T[]> newType)

复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。

static boolean[]copyOfRange(boolean[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static byte[]copyOfRange(byte[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static char[]copyOfRange(char[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static double[]copyOfRange(double[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static float[]copyOfRange(float[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static int[]copyOfRange(int[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static long[]copyOfRange(long[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static short[]copyOfRange(short[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static <T> T[]copyOfRange(T[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static <T,U> T[]copyOfRange(U[] original, int from, int to, <? extends T[]> newType)

将指定数组的指定范围复制到新数组中。

static booleandeepEquals(Object[] a1, Object[] a2)

如果两个指定的数组彼此 深度相等 ,则返回 true 。

static intdeepHashCode(Object[] a)

根据指定数组的“深度内容”返回哈希码。

static StringdeepToString(Object[] a)

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

static booleanequals(boolean[] a, boolean[] a2)

如果两个指定的布尔数组彼此 相等 ,则返回 true 。

static booleanequals(byte[] a, byte[] a2)

如果两个指定的字节数组彼此 相等 ,则返回 true 。

static booleanequals(char[] a, char[] a2)

如果两个指定的字符数组彼此 相等 ,则返回 true 。

static booleanequals(double[] a, double[] a2)

如果两个指定的双精度数组彼此 相等 ,则返回 true 。

static booleanequals(float[] a, float[] a2)

如果两个指定的浮动数组彼此 相等 ,则返回 true 。

static booleanequals(int[] a, int[] a2)

如果两个指定的int数组彼此 相等 ,则返回 true 。

static booleanequals(long[] a, long[] a2)

如果两个指定的longs数组彼此 相等 ,则返回 true 。

static booleanequals(Object[] a, Object[] a2)

如果两个指定的对象数组彼此 相等 ,则返回 true 。

static booleanequals(short[] a, short[] a2)

如果两个指定的短裤阵列彼此 相等 ,则返回 true 。

static voidfill(boolean[] a, boolean val)

将指定的布尔值分配给指定的布尔数组的每个元素。

static voidfill(boolean[] a, int fromIndex, int toIndex, boolean val)

将指定的布尔值分配给指定数组布尔值的指定范围的每个元素。

static voidfill(byte[] a, byte val)

将指定的字节值分配给指定字节数组的每个元素。

static voidfill(byte[] a, int fromIndex, int toIndex, byte val)

将指定的字节值分配给指定字节数组的指定范围的每个元素。

static voidfill(char[] a, char val)

将指定的char值分配给指定的char数组的每个元素。

static voidfill(char[] a, int fromIndex, int toIndex, char val)

将指定的char值分配给指定的char数组的指定范围的每个元素。

static voidfill(double[] a, double val)

将指定的double值分配给指定的双精度数组的每个元素。

static voidfill(double[] a, int fromIndex, int toIndex, double val)

将指定的double值分配给指定的双精度数组范围的每个元素。

static voidfill(float[] a, float val)

将指定的float值分配给指定的浮点数组的每个元素。

static voidfill(float[] a, int fromIndex, int toIndex, float val)

将指定的浮点值分配给指定的浮点数组的指定范围的每个元素。

static voidfill(int[] a, int val)

将指定的int值分配给指定的int数组的每个元素。

static voidfill(int[] a, int fromIndex, int toIndex, int val)

将指定的int值分配给指定的int数组的指定范围的每个元素。

static voidfill(long[] a, int fromIndex, int toIndex, long val)

将指定的long值分配给指定的longs数组的指定范围的每个元素。

static voidfill(long[] a, long val)

将指定的long值分配给指定的longs数组的每个元素。

static voidfill(Object[] a, int fromIndex, int toIndex, Object val)

将指定的对象引用分配给指定的对象数组的指定范围的每个元素。

static voidfill(Object[] a, Object val)

将指定的对象引用分配给指定的对象数组的每个元素。

static voidfill(short[] a, int fromIndex, int toIndex, short val)

将指定的短值分配给指定的短裤数组的指定范围的每个元素。

static voidfill(short[] a, short val)

将指定的短值分配给指定的短裤数组的每个元素。

static inthashCode(boolean[] a)

根据指定数组的内容返回哈希码。

static inthashCode(byte[] a)

根据指定数组的内容返回哈希码。

static inthashCode(char[] a)

根据指定数组的内容返回哈希码。

static inthashCode(double[] a)

根据指定数组的内容返回哈希码。

static inthashCode(float[] a)

根据指定数组的内容返回哈希码。

static inthashCode(int[] a)

根据指定数组的内容返回哈希码。

static inthashCode(long[] a)

根据指定数组的内容返回哈希码。

static inthashCode(Object[] a)

根据指定数组的内容返回哈希码。

static inthashCode(short[] a)

根据指定数组的内容返回哈希码。

static voidparallelPrefix(double[] array, DoubleBinaryOperator op)

使用提供的功能,并行地计算给定阵列的每个元素。

static voidparallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)

对于数组的给定子范围执行 parallelPrefix(double[], DoubleBinaryOperator)

static voidparallelPrefix(int[] array, IntBinaryOperator op)

使用提供的功能,并行地计算给定阵列的每个元素。

static voidparallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)

对于数组的给定子范围执行 parallelPrefix(int[], IntBinaryOperator)

static voidparallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)

对于数组的给定子范围执行 parallelPrefix(long[], LongBinaryOperator)

static voidparallelPrefix(long[] array, LongBinaryOperator op)

使用提供的功能,并行地计算给定阵列的每个元素。

static <T> voidparallelPrefix(T[] array, BinaryOperator<T> op)

使用提供的功能,并行地计算给定阵列的每个元素。

static <T> voidparallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)

对于数组的给定子范围执行 parallelPrefix(Object[], BinaryOperator)

static voidparallelSetAll(double[] array, IntToDoubleFunction generator)

使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。

static voidparallelSetAll(int[] array, IntUnaryOperator generator)

使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。

static voidparallelSetAll(long[] array, IntToLongFunction generator)

使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。

static <T> voidparallelSetAll(T[] array, IntFunction<? extends T> generator)

使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。

static voidparallelSort(byte[] a)

按照数字顺序排列指定的数组。

static voidparallelSort(byte[] a, int fromIndex, int toIndex)

按照数字顺序排列数组的指定范围。

static voidparallelSort(char[] a)

按照数字顺序排列指定的数组。

static voidparallelSort(char[] a, int fromIndex, int toIndex)

按照数字顺序排列数组的指定范围。

static voidparallelSort(double[] a)

按照数字顺序排列指定的数组。

static voidparallelSort(double[] a, int fromIndex, int toIndex)

按照数字顺序排列数组的指定范围。

static voidparallelSort(float[] a)

按照数字顺序排列指定的数组。

static voidparallelSort(float[] a, int fromIndex, int toIndex)

按照数字顺序排列数组的指定范围。

static voidparallelSort(int[] a)

按照数字顺序排列指定的数组。

static voidparallelSort(int[] a, int fromIndex, int toIndex)

按照数字顺序排列数组的指定范围。

static voidparallelSort(long[] a)

按照数字顺序排列指定的数组。

static voidparallelSort(long[] a, int fromIndex, int toIndex)

按照数字顺序排列数组的指定范围。

static voidparallelSort(short[] a)

按照数字顺序排列指定的数组。

static voidparallelSort(short[] a, int fromIndex, int toIndex)

按照数字顺序排列数组的指定范围。

static <T extends Comparable<? super T>>
void
parallelSort(T[] a)

对指定对象升序排列的阵列,根据natural ordering的元素。

static <T> voidparallelSort(T[] a, Comparator<? super T> cmp)

根据指定的比较器引发的顺序对指定的对象数组进行排序。

static <T extends Comparable<? super T>>
void
parallelSort(T[] a, int fromIndex, int toIndex)

对指定对象升序排列的数组的指定范围内,根据natural ordering的元素。

static <T> voidparallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)

根据指定的比较器引发的顺序对指定的对象数组的指定范围进行排序。

static voidsetAll(double[] array, IntToDoubleFunction generator)

使用提供的生成函数来计算每个元素,设置指定数组的所有元素。

static voidsetAll(int[] array, IntUnaryOperator generator)

使用提供的生成函数来计算每个元素,设置指定数组的所有元素。

static voidsetAll(long[] array, IntToLongFunction generator)

使用提供的生成函数来计算每个元素,设置指定数组的所有元素。

static <T> voidsetAll(T[] array, IntFunction<? extends T> generator)

使用提供的生成函数来计算每个元素,设置指定数组的所有元素。

static voidsort(byte[] a)

按照数字顺序排列指定的数组。

static voidsort(byte[] a, int fromIndex, int toIndex)

按升序排列数组的指定范围。

static voidsort(char[] a)

按照数字顺序排列指定的数组。

static voidsort(char[] a, int fromIndex, int toIndex)

按升序排列数组的指定范围。

static voidsort(double[] a)

按照数字顺序排列指定的数组。

static voidsort(double[] a, int fromIndex, int toIndex)

按升序排列数组的指定范围。

static voidsort(float[] a)

按照数字顺序排列指定的数组。

static voidsort(float[] a, int fromIndex, int toIndex)

按升序排列数组的指定范围。

static voidsort(int[] a)

按照数字顺序排列指定的数组。

static voidsort(int[] a, int fromIndex, int toIndex)

按升序排列数组的指定范围。

static voidsort(long[] a)

按照数字顺序排列指定的数组。

static voidsort(long[] a, int fromIndex, int toIndex)

按升序排列数组的指定范围。

static voidsort(Object[] a)

对指定对象升序排列的阵列,根据natural ordering的元素。

static voidsort(Object[] a, int fromIndex, int toIndex)

对指定对象升序排列的数组的指定范围内,根据natural ordering的元素。

static voidsort(short[] a)

按照数字顺序排列指定的数组。

static voidsort(short[] a, int fromIndex, int toIndex)

按升序排列数组的指定范围。

static <T> voidsort(T[] a, Comparator<? super T> c)

根据指定的比较器引发的顺序对指定的对象数组进行排序。

static <T> voidsort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)

根据指定的比较器引发的顺序对指定的对象数组的指定范围进行排序。

static Spliterator.OfDoublespliterator(double[] array)

返回Spliterator.OfDouble覆盖所有指定数组。

static Spliterator.OfDoublespliterator(double[] array, int startInclusive, int endExclusive)

返回Spliterator.OfDouble覆盖指定数组的指定范围内。

static Spliterator.OfIntspliterator(int[] array)

返回Spliterator.OfInt覆盖所有指定数组。

static Spliterator.OfIntspliterator(int[] array, int startInclusive, int endExclusive)

返回Spliterator.OfInt覆盖指定数组的指定范围内。

static Spliterator.OfLongspliterator(long[] array)

返回Spliterator.OfLong覆盖所有指定数组。

static Spliterator.OfLongspliterator(long[] array, int startInclusive, int endExclusive)

返回Spliterator.OfLong覆盖指定数组的指定范围内。

static <T> Spliterator<T>spliterator(T[] array)

返回Spliterator覆盖所有指定数组。

static <T> Spliterator<T>spliterator(T[] array, int startInclusive, int endExclusive)

返回Spliterator覆盖指定数组的指定范围内。

static DoubleStreamstream(double[] array)

返回顺序DoubleStream与指定的数组作为源。

static DoubleStreamstream(double[] array, int startInclusive, int endExclusive)

返回顺序DoubleStream与指定的数组作为源的指定范围。

static IntStreamstream(int[] array)

返回顺序IntStream与指定的数组作为源。

static IntStreamstream(int[] array, int startInclusive, int endExclusive)

返回顺序IntStream与指定的数组作为源的指定范围。

static LongStreamstream(long[] array)

返回顺序LongStream与指定的数组作为源。

static LongStreamstream(long[] array, int startInclusive, int endExclusive)

返回顺序LongStream与指定的数组作为源的指定范围。

static <T> Stream<T>stream(T[] array)

返回顺序Stream与指定的数组作为源。

static <T> Stream<T>stream(T[] array, int startInclusive, int endExclusive)

返回顺序Stream与指定的数组作为源的指定范围。

static StringtoString(boolean[] a)

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

static StringtoString(byte[] a)

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

static StringtoString(char[] a)

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

static StringtoString(double[] a)

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

static StringtoString(float[] a)

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

static StringtoString(int[] a)

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

static StringtoString(long[] a)

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

static StringtoString(Object[] a)

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

static StringtoString(short[] a)

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

3.大数值

3.1 java.math.BigDecimal

  • 不变的,任意精度的带符号的十进制数字。 A BigDecimal由任意精度整数未缩放值和32位整数级别组成 。 如果为零或正数,则刻度是小数点右侧的位数。 如果是负数,则数字的非标定值乘以10,以达到等级的否定的幂。 因此,BigDecimal所代表的BigDecimal值为(unscaledValue × 10-scale) 。

    BigDecimal类提供了算术,缩放操作,舍入,比较,散列和格式转换的操作。 toString()方法提供了一个BigDecimal的规范BigDecimal

    BigDecimal类使用户完全控制舍入行为。 如果未指定舍入模式,并且无法表示确切的结果,则抛出异常; 否则,可以通过向操作提供适当的MathContext对象来进行计算,以选择精度和舍入模式。 在这两种情况下,都提供了八种舍入方式来控制舍入。 使用此类中的整数字段(如ROUND_HALF_UP )表示舍入模式已经过时了; 应该使用RoundingMode 枚举 (如RoundingMode.HALF_UP )的枚举值。

域​​​​​​​

Modifier and TypeField and Description
static BigDecimalONE

值1,标度为0。

static intROUND_CEILING

圆形模式向正无穷大转弯。

static intROUND_DOWN

舍入模式向零舍入。

static intROUND_FLOOR

舍入模式向负无穷大转弯。

static intROUND_HALF_DOWN

四舍五入模式向“最近邻居”转弯,除非这两个邻居都是等距离的,在这种情况下,这是倒圆的。

static intROUND_HALF_EVEN

四舍五入模式向“最近邻居”转弯,除非两个邻居都是等距离的,在这种情况下,向着邻居方向转移。

static intROUND_HALF_UP

四舍五入模式向“最近邻居”转弯,除非两个邻居都是等距的,在这种情况下是圆括弧的。

static intROUND_UNNECESSARY

舍入模式来确定所请求的操作具有精确的结果,因此不需要舍入。

static intROUND_UP

舍入模式从零开始。

static BigDecimalTEN

值为10,标度为0。

static BigDecimalZERO

值为0,标度为0。

构造方法​​​​​​​

Constructor and Description
BigDecimal(BigInteger val)

BigInteger转换成 BigDecimal

BigDecimal(BigInteger unscaledVal, int scale)

将BigInteger的 BigInteger值和 int等级转换为 BigDecimal

BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)

BigInteger未缩放值和 int扩展转换为 BigDecimal ,根据上下文设置进行舍入。

BigDecimal(BigInteger val, MathContext mc)

根据上下文设置将 BigInteger转换为 BigDecimal舍入。

BigDecimal(char[] in)

一个转换的字符数组表示 BigDecimalBigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造。

BigDecimal(char[] in, int offset, int len)

一个转换的字符数组表示 BigDecimalBigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造,同时允许一个子阵列被指定。

BigDecimal(char[] in, int offset, int len, MathContext mc)

一个转换的字符数组表示 BigDecimalBigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造,同时允许指定一个子阵列和用根据上下文设置进行舍入。

BigDecimal(char[] in, MathContext mc)

一个转换的字符数组表示 BigDecimalBigDecimal ,接受相同的字符序列作为 BigDecimal(String)构造与根据上下文设置进行舍入。

BigDecimal(double val)

double转换为 BigDecimal ,这是 double的二进制浮点值的精确十进制表示。

BigDecimal(double val, MathContext mc)

double转换为 BigDecimal ,根据上下文设置进行舍入。

BigDecimal(int val)

intBigDecimal

BigDecimal(int val, MathContext mc)

int转换为 BigDecimal ,根据上下文设置进行舍入。

BigDecimal(long val)

longBigDecimal

BigDecimal(long val, MathContext mc)

long转换为 BigDecimal ,根据上下文设置进行舍入。

BigDecimal(String val)

将BigDecimal的字符串表示 BigDecimal转换为 BigDecimal

BigDecimal(String val, MathContext mc)

一个转换的字符串表示 BigDecimalBigDecimal ,接受相同的字符串作为 BigDecimal(String)构造,利用根据上下文设置进行舍入。

方法​​​​​​​

Modifier and TypeMethod and Description
BigDecimalabs()

返回一个 BigDecimal ,其值为此 BigDecimal的绝对值,其缩放比例为 this.scale()

BigDecimalabs(MathContext mc)

返回一个 BigDecimal ,其值为此 BigDecimal的绝对值,根据上下文设置进行舍入。

BigDecimaladd(BigDecimal augend)

返回 BigDecimal ,其值是 (this + augend) ,其标为 max(this.scale(), augend.scale())

BigDecimaladd(BigDecimal augend, MathContext mc)

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

bytebyteValueExact()

将此 BigDecimal转换为 byte ,检查丢失的信息。

intcompareTo(BigDecimal val)

将此 BigDecimal与指定的BigDecimal进行 BigDecimal

BigDecimaldivide(BigDecimal divisor)

返回BigDecimal ,其值为(this / divisor) ,优先级为(this.scale() - divisor.scale()) ; 如果不能表示确切的商(因为它具有非终止的十进制扩展),则抛出一个ArithmeticException

BigDecimaldivide(BigDecimal divisor, int roundingMode)

返回 BigDecimal ,其值是 (this / divisor) ,其标为 this.scale()

BigDecimaldivide(BigDecimal divisor, int scale, int roundingMode)

返回一个 BigDecimal ,其值为 (this / divisor) ,其比例为指定。

BigDecimaldivide(BigDecimal divisor, int scale, RoundingMode roundingMode)

返回一个 BigDecimal ,其值为 (this / divisor) ,其比例为指定。

BigDecimaldivide(BigDecimal divisor, MathContext mc)

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

BigDecimaldivide(BigDecimal divisor, RoundingMode roundingMode)

返回 BigDecimal ,其值是 (this / divisor) ,其标为 this.scale()

BigDecimal[]divideAndRemainder(BigDecimal divisor)

返回一个两元件 BigDecimal阵列含有的结果 divideToIntegralValue接着的结果 remainder上的两个操作数。

BigDecimal[]divideAndRemainder(BigDecimal divisor, MathContext mc)

返回一个两元件 BigDecimal阵列含有的结果 divideToIntegralValue接着的结果 remainder上与根据上下文设置进行舍入计算出的两个操作数。

BigDecimaldivideToIntegralValue(BigDecimal divisor)

返回一个 BigDecimal ,它的值是 BigDecimal的整数部分 (this / divisor)取整。

BigDecimaldivideToIntegralValue(BigDecimal divisor, MathContext mc)

返回值为 BigDecimal的整数部分的 (this / divisor)

doubledoubleValue()

将此 BigDecimal转换为 double

booleanequals(Object x)

将此 BigDecimal与指定的 Object进行比较以获得相等性。

floatfloatValue()

将此 BigDecimal转换为 float

inthashCode()

返回此 BigDecimal的哈希码。

intintValue()

将此 BigDecimal转换为 int

intintValueExact()

将此 BigDecimal转换为 int ,检查丢失的信息。

longlongValue()

将此 BigDecimal转换为 long

longlongValueExact()

将此 BigDecimal转换为 long ,检查丢失的信息。

BigDecimalmax(BigDecimal val)

返回此 BigDecimalval

BigDecimalmin(BigDecimal val)

返回此 BigDecimalval

BigDecimalmovePointLeft(int n)

返回一个 BigDecimal ,相当于这个小数点,向左移动了 n个地方。

BigDecimalmovePointRight(int n)

返回一个 BigDecimal ,相当于这个小数点移动了 n个地方。

BigDecimalmultiply(BigDecimal multiplicand)

返回 BigDecimal ,其值是 (this × multiplicand),其标为 (this.scale() + multiplicand.scale())

BigDecimalmultiply(BigDecimal multiplicand, MathContext mc)

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

BigDecimalnegate()

返回 BigDecimal ,其值是 (-this) ,其标为 this.scale()

BigDecimalnegate(MathContext mc)

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

BigDecimalplus()

返回 BigDecimal ,其值是 (+this) ,其标为 this.scale()

BigDecimalplus(MathContext mc)

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

BigDecimalpow(int n)

返回 BigDecimal ,其值是 (thisn),该电源,准确计算,使其具有无限精度。

BigDecimalpow(int n, MathContext mc)

返回 BigDecimal ,其值是 (thisn)。

intprecision()

返回此 BigDecimalBigDecimal

BigDecimalremainder(BigDecimal divisor)

返回 BigDecimal ,其值是 (this % divisor)

BigDecimalremainder(BigDecimal divisor, MathContext mc)

返回 BigDecimal ,其值是 (this % divisor) ,根据上下文设置进行舍入。

BigDecimalround(MathContext mc)

返回 BigDecimal根据四舍五入 MathContext设置。

intscale()

返回此 规模 BigDecimal

BigDecimalscaleByPowerOfTen(int n)

返回一个BigDecimal,其数值等于( this * 10 n )。

BigDecimalsetScale(int newScale)

返回一个 BigDecimal ,其大小是指定值,其值在数字上等于此 BigDecimal

BigDecimalsetScale(int newScale, int roundingMode)

返回一个 BigDecimal ,其规模是指定值,其缩放值通过将此 BigDecimal的非标度值乘以10的适当功率来确定,以维持其总体值。

BigDecimalsetScale(int newScale, RoundingMode roundingMode)

返回一个 BigDecimal ,其规模是指定值,其缩放值通过将该 BigDecimal的非标度值乘以10的适当功率来确定,以维持其整体值。

shortshortValueExact()

将此 BigDecimal转换为 short ,检查丢失的信息。

intsignum()

返回这个 BigDecimal的signum函数。

BigDecimalstripTrailingZeros()

返回一个 BigDecimal ,它在数字上等于此值, BigDecimal表示中删除任何尾随的零。

BigDecimalsubtract(BigDecimal subtrahend)

返回 BigDecimal ,其值是 (this - subtrahend) ,其标为 max(this.scale(), subtrahend.scale())

BigDecimalsubtract(BigDecimal subtrahend, MathContext mc)

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

BigIntegertoBigInteger()

将此 BigDecimal转换为 BigInteger

BigIntegertoBigIntegerExact()

将此 BigDecimal转换为 BigInteger ,检查丢失的信息。

StringtoEngineeringString()

如果需要指数,则使用工程符号返回此 BigDecimal的字符串表示形式。

StringtoPlainString()

返回没有指数字段的此 BigDecimal的字符串表示形式。

StringtoString()

返回此 BigDecimal的字符串表示,如果需要指数,则使用科学计数法。

BigDecimalulp()

返回此 BigDecimal的最后一个位置的ulp(一个单位)的大小。

BigIntegerunscaledValue()

返回一个 BigInteger ,其值是此 BigDecimal未缩放值

static BigDecimalvalueOf(double val)

转换一个 doubleBigDecimal ,使用 double通过所提供的规范的字符串表示 Double.toString(double)方法。

static BigDecimalvalueOf(long val)

long值转换为 BigDecimal ,比例为零。

static BigDecimalvalueOf(long unscaledVal, int scale)

long值和 int比例转换为 BigDecimal

3.2 java.math.BigInteger 

  • 不可变的任意精度整数。 所有操作的行为就好像BigIntegers以二进制补码表示(如Java的原始整数类型)表示。 BigInteger提供了所有Java的原始整数运算符和java.lang.Math中所有相关方法的类比。 此外,BigInteger还提供了模数运算,GCD计算,原始测试,初级生成,位操作以及其他一些其他操作的操作。

域​​​​​​​

Modifier and TypeField and Description
static BigIntegerONE

BigInteger常数。

static BigIntegerTEN

BigInteger常数十。

static BigIntegerZERO

BigInteger常数为零。

构造方法​​​​​​​

Constructor and Description
BigInteger(byte[] val)

将包含BigInteger的二进制补码二进制表达式的字节数组转换为BigInteger。

BigInteger(int signum, byte[] magnitude)

将BigInteger的符号大小表示形式转换为BigInteger。

BigInteger(int bitLength, int certainty, Random rnd)

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

BigInteger(int numBits, Random rnd)

构造一个随机生成的BigInteger,均匀分布在0到(2 numBits - 1)的范围内。

BigInteger(String val)

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

BigInteger(String val, int radix)

将指定基数中的BigInteger的String表示形式转换为BigInteger。

方法​​​​​​​

Modifier and TypeMethod and Description
BigIntegerabs()

返回一个BigInteger,它的值是此BigInteger的绝对值。

BigIntegeradd(BigInteger val)

返回值为 (this + val)

BigIntegerand(BigInteger val)

返回值为 (this & val)

BigIntegerandNot(BigInteger val)

返回值为 (this & ~val)

intbitCount()

返回与其符号位不同的BigInteger的二进制补码表示中的位数。

intbitLength()

返回此BigInteger的最小二进制补码表示中的位数, 包括符号位。

bytebyteValueExact()

将此 BigInteger转换为 byte ,检查丢失的信息。

BigIntegerclearBit(int n)

返回一个BigInteger,其值等于此BigInteger,指定的位被清零。

intcompareTo(BigInteger val)

将此BigInteger与指定的BigInteger进行比较。

BigIntegerdivide(BigInteger val)

返回值为 (this / val)

BigInteger[]divideAndRemainder(BigInteger val)

返回两个BigInteger的数组,其中包含 (this / val)后跟 (this % val)

doubledoubleValue()

将此BigInteger转换为 double

booleanequals(Object x)

将此BigInteger与指定的对象进行比较以实现相等。

BigIntegerflipBit(int n)

返回一个BigInteger,其值等于此BigInteger,指定的位被翻转。

floatfloatValue()

将此BigInteger转换为 float

BigIntegergcd(BigInteger val)

返回一个BigInteger,其值是 abs(this)abs(val)

intgetLowestSetBit()

返回此BigInteger中最右(最低位)一位的索引(最右边一位右侧的零位数)。

inthashCode()

返回此BigInteger的哈希码。

intintValue()

将此BigInteger转换为 int

intintValueExact()

将此 BigInteger转换为 int ,检查丢失的信息。

booleanisProbablePrime(int certainty)

返回 true如果这个BigInteger可能是素数, false如果它是绝对复合。

longlongValue()

将此BigInteger转换为 long

longlongValueExact()

将此 BigInteger转换为 long ,检查丢失的信息。

BigIntegermax(BigInteger val)

返回此BigInteger和 val

BigIntegermin(BigInteger val)

返回此BigInteger和 val

BigIntegermod(BigInteger m)

返回值为 (this mod m )。

BigIntegermodInverse(BigInteger m)

返回值为 (this -1 mod m)

BigIntegermodPow(BigInteger exponent, BigInteger m)

返回值为 (thisexponent mod m)的BigInteger 。

BigIntegermultiply(BigInteger val)

返回值为 (this * val)

BigIntegernegate()

返回值为 (-this)

BigIntegernextProbablePrime()

返回大于这个 BigIntegerBigInteger的第一个整数。

BigIntegernot()

返回值为 (~this)

BigIntegeror(BigInteger val)

返回值为 (this | val)

BigIntegerpow(int exponent)

返回值为 (thisexponent)的BigInteger 。

static BigIntegerprobablePrime(int bitLength, Random rnd)

返回一个正的BigInteger,它可能是素数,具有指定的位长度。

BigIntegerremainder(BigInteger val)

返回值为 (this % val)

BigIntegersetBit(int n)

返回一个BigInteger,其值等于具有指定位集合的BigInteger。

BigIntegershiftLeft(int n)

返回值为 (this << n)

BigIntegershiftRight(int n)

返回值为 (this >> n)

shortshortValueExact()

将此 BigInteger转换为 short ,检查丢失的信息。

intsignum()

返回此BigInteger的signum函数。

BigIntegersubtract(BigInteger val)

返回值为 (this - val)

booleantestBit(int n)

返回 true当且仅当指定的位被设置。

byte[]toByteArray()

返回一个包含此BigInteger的二进制补码表示的字节数组。

StringtoString()

返回此BigInteger的十进制字符串表示形式。

StringtoString(int radix)

返回给定基数中BigInteger的String表示形式。

static BigIntegervalueOf(long val)

返回一个BigInteger,其值等于指定的 long

BigIntegerxor(BigInteger val)

返回值为 (this ^ val)

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值