day07

Day07

1. Background

今天是学习java的第七天,今天我学习到了数组。

2. Description

2.1 About

数组是一种数据结构,用来存储同一类型值的集合。具体的表现方式和C语言和很像,在此就不再赘述。

需要注意的是java声明数组和C语言不同

int[] a;
int[] a = new int[100];

在上面代码中,第一句声明了数组变量,其中数据类型紧跟[],这里与C语言有很大的不同。但是这句只声明了变量a,并没有将a初始化为真正的数组,所以要用 new 运算符来创建数组.

PS:想C语言那样[]在变量名之后的方法也可以创建数组,只不过java程序员更多的喜欢使用 datetype[] 的方法

当创建一个数字数组时,所有元素都会被初始化为0,如果是Boolean类型数组,则会被初始化false。对象数组的元素则被初始化为一个特殊值null,表示这些元素还没有任何对象。

一旦创建了数组就不可再改变数组大小,如果想要在运行得时候改变数组大小,可以使用另一种数据结构——数组列表(array list)

2.2 java.util.Arrays

这是java常用得一个库,如名字一样,用于数组的运算。

  1. sort()

    public static void sort(int[] a) {}
    public static void sort(int[] a, int fromIndex, int toIndex) {}
    

    只有一个参数的sort()对给定的数组参数按照升序排序。有三个参数的sort()对第一个数组参数的指定索引范围进行排序,该范围由后两个参数指定,从第二个参数fromIndex到第三个参数toIndex(不包含该索引)。

    Arrays中的sort()方法对除boolean外的所有 7 基本数据类型进行了重载,所以这里的int可以替换为其它 6 种基本数据类型种的任何一种。这些sort()方法使用的排序算法属于快速排序。

    public static void sort(Object[] a) {}
    public static void sort(Object[] a, int fromIndex, int toIndex) {}
    

    输入参数为Object类型的sort()方法,所有参数数组中的元素都必须实现Comparable接口,并且所有的元素之间必须可以相互比较(即e1.compareTo(e2)对任意的两个数组元素e1e2都不能产生异常ClassCastException)。其排序方式属于归并排序。

    public static <T> void sort(T[] a, Comparator<? super T> c) {}
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
                                Comparator<? super T> c) {}
    

    这是使用泛型的sort()方法,它需要根据给定的比较器Comparator所得到的顺序对给定数组进行排序。其排序方式属于归并排序。

  2. parallelSort()

    public static void parallelSort(byte[] a) {}
    public static void parallelSort(byte[] a, int fromIndex, int toIndex) {}
    

    parallelSort()的各参数意义同sort()一样,返回数组的升序排序结果。parallelSort()通过并行的方式使用归并排序,它将原数组不断分解为子数组,分别对子数组进行排序。当子数组长度达到一个最小粒度时,则通过适当的Array.sort()方法对其进行排序。该算法需要一个不大于原始数组大小的工作空间。

    sort()一样,Arrays中的parallelSort()方法对除boolean外的所有 7 基本数据类型进行了重载,所以这里的byte可以替换为其它 6 种基本数据类型种的任何一种。

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

    输入参数为泛型的parallelSort()方法,所有参数数组中的元素都必须实现Comparable接口,并且所有的元素之间必须可以相互比较(即e1.compareTo(e2)对任意的两个数组元素e1e2都不能产生异常ClassCastException)。其排序方式属于归并排序。

    public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {}
    public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
                                            Comparator<? super T> cmp) {}
    

    这是使用泛型的parallelSort()方法,它需要根据给定的比较器Comparator所得到的顺序对给定数组进行排序。其排序方式属于归并排序。

  3. parallelPrefix()

    public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) {}
    public static <T> void parallelPrefix(T[] array, int fromIndex,
                                          int toIndex, BinaryOperator<T> op) {
    

    使用提供的函数操作,对数组进行累积操作,相当于连续空间种的积分。比如输入的原始数组是[2, 1, 0, 3],那么累积操作后的数组是[2, 3, 3, 6]。对于含有两个参数的parallelPrefix,第一个参数array是原始数组,第二个参数op提供执行累积操作的函数。对于含有四个参数的parallelPrefixarray是原始数组,第二和第三个参数指定进行累积操作数组范围为从fromIndextoIndex(不包含该索引),第四个参数op提供执行累积操作的函数。

    Arrays还提供了这两种形式的parallelPrefix()方法对于int/long/double的重载方法,参数意义和方法作用类似。

  4. binarySearch()

    // 使用二分搜索在整个有序数组 a 中寻找 key 值的所在位置索引
    public static int binarySearch(long[] a, long key) {}
    // 使用二分搜索在整个有序数组 a 的指定范围 [fromIndex, toIndex) 中寻找 key 值的所在位置索引
    public static int binarySearch(long[] a, int fromIndex, int toIndex, long key) {}
    

    使用二分搜索在有序数组a中寻找指定参数key值的所在位置索引。如果找得到,则返回找到的索引值;如果没有找到,则返回值为(-(insertion point)-1),这里的insertion point就是key应该插入到有序数组a中的位置,保证数组a仍然有序。这个返回方式确实有些怪异,它的目的是保证只有在数组a中找到key时,返回值才一定时整数,而没找到情况下返回值一定是负数(该负数包含了key的插入点信息)。

    Arrays中的binarySearch()方法对除boolean外的所有 7 基本数据类型进行了重载,所以这里的long可以替换为其它 6 种基本数据类型种的任何一种。

    Arrays还提供了binarySearch()方法对Object类型以及泛型的重载方法。提供的方式和sort()类似,具体形式如下。

    public static int binarySearch(Object[] a, Object key){}
    public static binarySearch(Object[] a, int fromIndex, int toIndex, Object key) {}
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {}
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) {}
    
  5. equals()

    // 判断对于两个数组中的所有元素时候满足相等的要求。
    public static boolean equals(long[] a, long[] a2) {}
    // 判断对于两个参数指定范围中的元素是否满足相等的要求。
    public static boolean equals(long[] a, int aFromIndex, int aToIndex,
                                     long[] b, int bFromIndex, int bToIndex) {
    

    判断两个数组是否相等,它的涵义是,两个数组中是否包含相同的元素且排列顺序也相同。

    Arrays提供了equals()方法对 8 种基本数据类型的重载方法。

    Arrays还提供了equals()方法对Object类型以及泛型的重载方法。作用相同,不再赘述。对于使用泛型的重载方法,需要提供一个Comparator<? super T>参数作为比较器判断两个元素是否相等,cmp.compare(e1, e2) == 0表示e1e2相等。

    public static boolean equals(Object[] a, Object[] a2) {}
    public static boolean equals(Object[] a, int aFromIndex, int aToIndex,
                                 Object[] b, int bFromIndex, int bToIndex) {}
    public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) {}
    public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex,
                                     T[] b, int bFromIndex, int bToIndex,
                                     Comparator<? super T> cmp) {
    
  6. fill()

    public static void fill(long[] a, long val) {}
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {}
    

    使用指定数值val填充数组a。对于两个参数的fill()方法,表示使用val赋值a中的每个元素。对于四个参数的fill()方法,表示使用val赋值指定a中范围的元素,即从fromIndextoIndex(不包含该索引)。

    Arrays提供了fill()方法对 8 种基本数据类型以及Object类型的重载方法。作用相同,不再赘述。

  7. copyOf()

    public static boolean[] copyOf(boolean[] original, int newLength) {}
    

    复制数组original到长度为newLength的新数组中。在原数组和新数组中同时有效的索引处,两者具有相等的元素值,在新数组中有效而在原数组中无效的索引处,元素置为默认值。

    Arrays提供了copyOf()方法对 8 种基本数据类型以及泛型的重载方法。作用相同,不再赘述。

  8. copyOfRange()

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

    复制数组original指定范围(从fromto(不包含to索引))的元素到新数组中。from的值必须处于 0 到 original.length之间,它对应于新数组的起始索引。to的值必须大于或等于from的值,to可以超过original.length,超出原数组范围的索引,其对应的新数组中的索引处元素置为默认值。

    Arrays提供了copyOf()方法对 8 种基本数据类型以及泛型的重载方法。作用相同,不再赘述。

  9. asList()

    public static <T> List<T> asList(T... a) {}
    

    通过给定数组a得到一个定长的List对象,即将一个数组转换为一个List。此方法还可以方便地创建一个初始状态包含多个元素的定长List对象,像这样:

    List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
    
  10. hashCode()

    public static int hashCode(long a[]) {}
    

    根据参数数组a的内容计算一个哈希码。

    对于任意两个数组abhashCode(a)==hashCode(b)Arrays.equals(a, b)结果相同。

    该方法返回的哈希码,跟包含与a相同元素且排列顺序相同的List对象调用hashCode()方法得到的哈希码,具有相同的值。如果anull,则方法返回 0。

    Arrays提供了hashCode()方法对 8 种基本数据类型以及Object类型的重载方法。作用相同,不再赘述。

  11. deepHashCode()

    public static int deepHashCode(Object a[]) {}
    

    根据参数数组a的深层内容计算一个哈希码。如果数组a的元素也是数组,哈希值的计算也会考虑到该数组的内容。

    对于任意两个数组abdeepHashCode(a)==deepHashCode(b)Arrays.deepEquals(a, b)结果相同。

  12. deepEquals()

    public static boolean deepEquals(Object[] a1, Object[] a2) {}
    

    判断两个数组a1a2是否深层相等。返回true则相等,返回false则不相等。与equals()方法不同,该方法适用于任意深度嵌套的数组。

  13. toString()

    public static String toString(long[] a) {}
    

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

    Arrays提供了toString()方法对 8 种基本数据类型以及Object类型的重载方法。作用相同,不再赘述。

  14. deepToString()

    public static String deepToString(Object[] a) {}
    

    返回数组a的深层内容的字符串表示形式。就是说,如果数组a的元素也是数组,那么返回的字符串中也表示除了其包含的内容。

    如果数组a的某个元素是它本身的引用,返回的字符串中对应位置表示为[...]的形式。

  15. setAll()

    public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {}
    

    通过给定的生成器函数generator,设置数组array的所有元素。

    Arrays提供了``setAll()方法对int/long/double`类型的重载方法。

  16. parallelSetAll()

    public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {}
    

    通过给定的生成器函数generator,并行地设置数组array的所有元素。

    Arrays提供了parallelSetAll()方法对int/long/double类型的重载方法。

  17. spliterator()

    // 返回一个覆盖整个数组的 Spliterator 对象
    public static <T> Spliterator<T> spliterator(T[] array) {}
    // 回一个覆盖数组指定范围(从 startInclusive 到 endExclusive (不包含该索引))的 Spliterator 对象。
    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {}
    

    返回一个覆盖数组arraySpliterator对象。

    Arrays提供了spliterator()方法对int/long/double类型的重载方法。

  18. stream()

    // 返回一个以整个数组作为源的 Stream 对象
    public static <T> Stream<T> stream(T[] array) {}
    // 返回一个以数组指定范围(从 startInclusive 到 endExclusive(不包含该索引))作为源的 Stream 对象
    public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) {}
    

    返回以数组array作为源的顺序Stream

    Arrays提供了stream()方法对int/long/double类型的重载方法。

以下是从 Java 9 开始加入的方法:

  1. compare()

    // 按字典序比较两个数组 a 和 b
    public static int compare(boolean[] a, boolean[] b) {}
    // 按字典序比较数组 a 的指定范围 [aFromIndex, aToIndex) 和 b 的指定范围 [bFromIndex, bToIndex)
    public static int compare(boolean[] a, int aFromIndex, int aToIndex,
                              boolean[] b, int bFromIndex, int bToIndex) {}
    

    按字典序比较两个数组ab。简单来说,就是比较两数组按索引从前到后第一个不相同的元素值之间的大小。返回 0 表示连个数组含有相同值且按相同顺序排列的元素;返回负数值表示第一个参数数组按字典序小于第二个参数数组;返回正数值表示第一个参数数组按字典序大于第二个参数数组。

    Arrays提供了compare()方法对 8 种基本数据类型的重载方法。

    Arrays提供了compare()方法使用泛型的重载方法,形式如下:

    // 前提是 T[] 中的元素都是可比较的
    public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {}
    public static <T extends Comparable<? super T>> int compare(
                T[] a, int aFromIndex, int aToIndex,
                T[] b, int bFromIndex, int bToIndex) {}
    // 使用给定的比较器 cmp 对数组中的元素进行比较
    public static <T> int compare(T[] a, T[] b,
                                      Comparator<? super T> cmp) {}
    public static <T> int compare(
                T[] a, int aFromIndex, int aToIndex,
                T[] b, int bFromIndex, int bToIndex,
                Comparator<? super T> cmp) {}
    
  2. compareUnsigned()

    public static int compareUnsigned(byte[] a, byte[] b) {}
    public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
    byte[] b, int bFromIndex, int bToIndex) {}
    

    作用与compare()差不多,区别是compareUnsigned()方法将数组中的元素视为无符号数。

    Arrays提供了compareUnsigned()方法对byte/short/int/long四种有符号数据类型的重载方法。

  3. mismatch()

    // 将数组元素看作无符号数,按字典序比较两个数组 a 和 b
    public static int mismatch(boolean[] a, boolean[] b) {}
    // 将数组元素看作无符号数,按字典序比较数组 a 的指定范围 [aFromIndex, aToIndex) 和 b 的指定范围 [bFromIndex, bToIndex)
    public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,
    boolean[] b, int bFromIndex, int bToIndex) {}
    

    找到两个数组ab按照索引顺序从前到后第一个不相同的元素位置,返回该位置的索引。如果没有找到不同的元素,则返回 -1,这表示两数组完全相同。

    Arrays提供了mismatch()方法对 8 种基本数据类型的重载方法。

    Arrays提供了mismatch()方法对Object类型以及使用泛型的重载方法,形式如下:

    public static int mismatch(Object[] a, Object[] b) {}
    public static int mismatch(
    Object[] a, int aFromIndex, int aToIndex,
    Object[] b, int bFromIndex, int bToIndex) {}
    public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {}
    public static <T> int mismatch(
    T[] a, int aFromIndex, int aToIndex,
    T[] b, int bFromIndex, int bToIndex,
    Comparator<? super T> cmp) {}
    

以上库中的函数均来自网络:https://www.cnblogs.com/alterwl/p/15187581.html

今天的代码中,用到的函数是deepToString,作用是把数组以字符串的形式打印出来。

3. Code

package basic;

import java.util.Arrays;

/**
 * This is the seventh code. Names and comments should follow my style stritly.
 * 
 * @author Fan Min minfanphd@163.com.
 */

public class Day07 {
    /**
     *****************
     * The entrance of the program.
     * 
     * @param args Not used now.
     *****************
     */
    public static void main(String[] args) {
        matrixElementSumTest();

        matrixAdditionTest();
    } // Of main

    /**
     *****************
     * Sum the elements of a matrix.
     * 
     * @param paraMatrix The given matrix.
     * @return The sum of all its elements.
     *****************
     */
    public static int matrixElementSum(int[][] paraMatrix) {
        int resultSum = 0;
        for (int i = 0; i < paraMatrix.length; i++) {
            for (int j = 0; j < paraMatrix[0].length; j++) {
                resultSum += paraMatrix[i][j];
            } // Of for j
        } // Of for i

        return resultSum;
    } // Of matrixElementSum

    /**
     *****************
     * Unit test for respective method.
     *****************
     */
    public static void matrixElementSumTest() {
        int[][] tempMatrix = new int[3][4];
        for (int i = 0; i < tempMatrix.length; i++) {
            for (int j = 0; j < tempMatrix[0].length; j++) {
                tempMatrix[i][j] = i * 10 +j;
            } // Of for j
        } // Of for i

        System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
        System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
    } // Of matrixElementSumTest

    /**
     *****************
     * Add two matrices.Attention: NO error check is provided at this moment.
     * 
     * @param paraMatrix1 The first matrix.
     * @param paraMatrix2 The second matrix. It should have the same size as the first one's.
     * @return The addition of the these matrices.
     *****************
     */
    public static int[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) {
        int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix1[0].length];
        
        for (int i = 0; i < paraMatrix1.length; i++) {
            for (int j = 0; j < paraMatrix1[0].length; j++) {
                resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
            } // Of for j
        } // Of for i

        return resultMatrix;
    } // Of matrixAddition

    /**
     ****************
     * Unit test for respective method.
     ****************
     */
    public static void matrixAdditionTest() {
        int[][] tempMatrix = new int[3][4];
        for (int i = 0; i < tempMatrix.length; i++) {
            for (int j = 0; j < tempMatrix[0].length; j++) {
                tempMatrix[i][j] = i * 10 +j;
            } // Of for j
        } // Of for i

        System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
        int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
        System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
    } // Of matrixAdditionTest
} // Of class Day07

运行结果:

在这里插入图片描述

4. Summarize

通过今天的学习,了解到了java中数组的用法和arrays库的使用。

总的来说,数组的声明和C语言有一些出入之外,其余的都很类似,然后不得不说java比C语言方便多了。在java中直接array.length就得出了数组的长度(在二维数组中这个求出来的是数组的列数),这个属实太方便了,以往写C语言的时候,还要专门声明一个变量,然后用函数求出要算的数组的长度,不胜其烦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值