java核心技术卷阅读笔记四_大数值_数组

一 : 大数值

  • 如果基本的整数和浮点数精度不能够满足需求, 那么可以使用java.math 包中的两个 很有用的类:Biglnteger 和 BigDecimal 这两个类可以处理包含任意长度数字序列的数值。 Biglnteger类实现了任意精度的整数运算, BigDecimal 实现了任意精度的浮点数运算。
  • 使用静态的 valueOf方法可以将普通的数值转换为大数值:
Biglnteger a = Biglnteger.valueOf(100);
  • 不能使用人们熟悉的算术运算符(如:+ 和 *) 处理大数值。 而需要使用大数 值类中的 add 和 multiply 方法。
Biglnteger c = a.add(b); // c = a + b 
Biglnteger d = c.nultipiy(b.add(Biglnteger.valueOf(2))); // d = c * (b + 2)

API

java.math.Biglnteger 1.1

  • Biglnteger add(Biglnteger other)
  • Biglnteger subtract(Biglnteger other)
  • Biglnteger multipiy(Biginteger other)
  • Biglnteger divide(Biglnteger other)
  • Biglnteger mod(Biglnteger other)
    返冋这个大整数和另一个大整数 other的和、差、 积、 商以及余数。
  • int compareTo(Biglnteger other)
    如果这个大整数与另一个大整数 other 相等, 返回 0;
    如果这个大整数小于另一个大整 数 other, 返回负数;
    否则, 返回正数。
  • static Biglnteger valueOf(long x)
    返回值等于 x 的大整数。

java.math.BigDecimal 1.1

  • BigDecimal add(BigDecimal other)
  • BigDecimal subtract(BigDecimal other)
  • BigDecimal multipiy(BigDecimal other)
  • BigDecimal divide(BigDecimal other RoundingMode mode) 5.0
    返回这个大实数与另一个大实数 other 的和、 差、 积、 商。要想计算商, 必须给出舍 入方式 (rounding mode)。RoundingMode.HALF_UP 是在学校中学习的四舍五入方式 ( 数值 0 到 4 舍去, 数值 5 到 9 进位) 。它适用于常规的计算。有关其他的舍入方 式请参看 Apr文档。
  • int compareTo(BigDecimal other)
    如果这个大实数与另一个大实数相等, 返回 0 ;
    如果这个大实数小于另一个大实数, 返回负数;
    否则,返回正数。
  • static BigDecimal valueOf(long x)
  • static BigDecimal valueOf(long x,int scale)
    返回值为 X 或 x / 10scale 的一个大实数。

二: 数组

2.1 声明数组

  • 声明整型数组a,没有赋值
int[] a;
  • 使用 new 运算 符创建数组。
  • 可以存储100个整数
  • 下标 0~99
int[ ] a = new int[100];
  • 当我们创建了一个指定数据类型和数组长度的时候,如果没有给数组中的元素赋值,则:
    • String类型默认为null.
    • 数字数组默认为0.
    • boolean 数组的元素会初始化为 false.
    • 对象数组的元素则初始化为一个特殊值 null,
  • 一旦创建了数组, 就不能再改变它的大小(长度).

2.2 数组初始化以及匿名数组

  • 在 Java中,提供了一种创建数组对象并同时赋予初始值的简化书写形式
int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
  • 还可以初始化一个匿名的数组:
new intD { 17, 19, 23, 29, 31, 37} 
  • 在 Java 中, 允许数组长度为 0。

2.3 数组拷贝

  • 在 Java中,允许将一个数组变量拷贝给 另一个数组变量。这时, 两个变量将引用同 一个数组:
        int[] arr1 = {1,2,3,4,5};
        int[] arr2= arr1;
        System.out.println(arr2[2]);// 3
  • 如果希望将 一个数组的所有值拷贝到一个新的数组中去, 就要使用 Arrays 类的 copyOf方法:
  • 第 2 个参数是新数组的长度。这个方法通常用来增加数组的大小:
  • 如果数组元素是数值型,那么多余的元素将被赋值为 0 ; 如果数组元素是布尔型,则将赋值 为 false。相反,如果长度小于原始数组的长度,则只拷贝最前面的数据元素。
        int[] arr1 = {1,2,3,4,5};
        int[] copiedLuckyNumbers = Arrays.copyOf(arr1, arr1.length);

2.4 数组排序

  • 要想对数值型数组进行排序, 可以使用 Arrays类中的 sort 方法:
int[] a = new int[10000]; 
...........
Arrays.sort(a) 
  • 这个方法使用了优化的快速排序算法
  • 从 49 个数值中抽取 6 个数,数组中元素不能重复:
	public static void main(String[] args) throws IOException {

        int n = 50;
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = i + 1;
        }
        System.out.println(Arrays.toString(numbers));

        int[] result = new int[6];
        for (int i = 0; i < result.length; i++) {
            //  0 到 1 之间(包含 0、 不包含 1 ) 的随机浮点数
            int num = (int) (Math.random() * n); // 返回 0 ~ 49 的整数
            result[i] = numbers[num];
           // 必须确保不会再次抽取到那个数值,因为所有抽彩的数值必须不相同。
            // 因此,这 里用数组中的最后一个数值改写number[num],并将 n 减 1。
            numbers[num] = numbers[n - 1];
            n--;
        }
        Arrays.sort(result);
        System.out.println(Arrays.toString(result));
    }

2.5 API

java.util.Arrays 1.2

  • static String toString(type[] a) 5.0
    返回包含 a 中数据元素的字符串, 这些数据元素被放在括号内, 并用逗号分隔。
    参数: a 类型为 int、long、short、char、 byte、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, 数组元素为 a 的值。
    参数:
    a 类型为 int、 long、short、char、byte、boolean、float 或 double 的数组。
    start 起始下标(包含这个值)0
    end 终止下标(不包含这个值)。这个值可能大于 a.length。在这种情况 下,结果为 0 或 false。
    length 拷贝的数据元素长度, 如果 length 值大于 a.length, 结果为 0 或 false ; 否则, 数组中只有前面 length 个数据元素的拷贝值。
  • static void sort(type[ ] a)
    采用优化的快速排序算法对数组进行排序。
    参数:a 类型为 int、long、short、char、byte、boolean、float 或 double 的数组。
  • static int binarySearch(type[] a, type v)
  • static int binarySearch(type[] a, int start, int end, type v) 6
    采用二分搜索算法查找值 v。如果查找成功, 则返回相应的下标值; 否则, 返回一个 负数值 r。-r-1 是为保持 a 有序 v 应插入的位置。
    参数:
    a 类型为 int、 long、short、 char、 byte、boolean、float 或 double 的有 序数组。
    start 起始下标(包含这个值) 。
    end 终止下标(不包含这个值)。
    v 同 a 的数据元素类型相同的值。
  • static void fill(type[] a, type v)
    将数组的所有数据元素值设置为 V。
    参数:
    a 类型为 int、long、short、char、byte、boolean、float 或 double 的数组。
    v 与 a 数据元素类型相同的一个值。
  • static boolean equals(type[] a, type[] b)
    如果两个数组大小相同, 并且下标相同的元素都对应相等, 返回 true。
    参数:a、 b 类型为 int、long、short、char、byte、boolean、float 或 double 的两个数组。

三 多维数组(跳过)

四 不规则数组(跳过)

猜你喜欢

转载自blog.csdn.net/weixin_42430194/article/details/86572637
今日推荐