Java数组常用的工具方法和字段(Java Array‘s common methods and fields)

2023-05-16

1 Mind Mapping of Java Array’s common methods and fields

在这里插入图片描述

2 Practical Code Demo

package com.test;

import java.util.Arrays;

public class JavaArrayUsage {
    //This statement only declares a integer array variable `a`. It does not yet initialize `a` with an actual array.
    static int[] nullIntArray;

    public static void main(String[] args) {
        //Print "null" as it isn't initialized
        System.out.println(nullIntArray);

        /*"int tempArray[]" is equivalent to "int[] tempArray", but most Java programmers prefer the former style
          because it neatly separates the type int[] (integer array) from the variable name.*/
        int tempArray[] = null;

        //It is legal to have arrays of length 0. Note that an array of length 0 is not the same as null.
        int[] emptyIntArray = new int[0];
        int[] emptyIntArray2 = new int[]{};
        /*"new int[]" is illegal*/
        //int[] emptyIntArray2 = new int[];
        /*Print "[] 0"*/
        System.out.println(Arrays.toString(emptyIntArray) + " " + emptyIntArray.length);
        /*Print "[] 0"*/
        System.out.println(Arrays.toString(emptyIntArray2) + " " + emptyIntArray2.length);

        //"int[] intArray = {1, 2, 3}" is shorthand for "int[] intArray = new int[]{1, 2, 3}"
        int[] intArray = {1, 2, 3};
        int[] intArray2 = new int[]{1, 2, 3};
        /*This statement declares and initializes an array of 3 integers, and every element of intArray3 is 0
          When you create an array of numbers, all elements are initialized with zero.
          Arrays of boolean are initialized with false. Arrays of objects are initialized with the special value null.*/
        int[] intArray3 = new int[3];
        int arrayLength = 3;
        //The array length need not be a constant before initializing:`new int[arrayLength]` creates an array of length arrayLength.
        int[] intArray4 = new int[arrayLength];
        /*Illegal statements*/
        //int[] intArray5 = new int[3]{1, 2, 3};
        //int[] intArray6 = new int[3]{};

        //To find th number of elements of an array, use "array.length"
        for (int i = 0; i < intArray.length; i++) {
            System.out.println(intArray[i]);
        }
        /*The "for each" loop is called the enhanced for loop. The collection expression must be an array or an
          object of a class that implements the Iterable interface, such as ArrayList. iterable [ˈɪtərəbl]a可迭代的
          You should read this loop as "for each j in intArray".*/
        for (int j : intArray) {
            System.out.println(j);
        }
        //Print "[I@45ee12a7"
        System.out.println(intArray);
        /*The call "Arrays.toString(intArray)" returns a string containing the array elements, such as "[1, 2, 3]"*/
        //Print "[1, 2,3]"
        System.out.println(Arrays.toString(intArray));

        /*Use the "copyOf" method in the Arrays class to copy all values of one array into a new array.
          The additional elements are filled with 0 if the array contains numbers, false if the array
          contains boolean values.*/
        int[] luckyNumbers = {1, 2};
        luckyNumbers = new int[]{1, 3};
        int[] copiedLuckyNumbers = Arrays.copyOf(luckyNumbers, luckyNumbers.length * 2);
        //Print "[1, 3, 0, 0]"
        System.out.println(Arrays.toString(copiedLuckyNumbers));

        int[] unsortedArray = {4, 3, 5, 2};
        //"Arrays.sort()" sorts the specified array into ascending numerical order by the dual-pivot Quicksort.
        Arrays.sort(unsortedArray);
        //Print "[2, 3, 4, 5]"
        System.out.println(Arrays.toString(unsortedArray));

        int[] intArray7 = {1, 2, 3, 5};
        /*"Arrays.binarySearch()" searches the specified array of ints for the specified value using the binary search algorithm.
          The array must be sorted prior to making this call.  If it is not sorted, the results are undefined.  If the array contains
          multiple elements with the specified value, there is no guarantee which one will be found. The method returns the index of
          the search key, if it is contained in the array, otherwise returns a negative number, if it isn't contained in the array.*/
        int searchedNumber3Index = Arrays.binarySearch(intArray7, 3);
        //Print "the index of value 3 in array [1, 2, 3, 5] is 2"
        System.out.println("the index of value 3 in array [1, 2, 3, 5] is " + searchedNumber3Index);
        int searchedNumber4Index = Arrays.binarySearch(intArray7, 4);
        //Print "the index of value 4 in array [1, 2, 3, 5] is -4"
        System.out.println("the index of value 4 in array [1, 2, 3, 5] is " + searchedNumber4Index);

        int[] intArray8 = {1, 3, 5};
        int[] intArray9 = {1, 3, 5};
        //Print "false"
        System.out.println(intArray8 == intArray9);
        /*Print "true", "Arrays.equals()" returns true if the two specified arrays of ints are equal to one another. Two arrays are considered
          equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.*/
        System.out.println(Arrays.equals(intArray8, intArray9));

        //Multi-dimensional Arrays
        int[][] twoDimenArray = {
                {1, 3, 2},
                {4, 8, 6}
        };
        /*A "for each" loop does not automatically loop through all elements in a two-dimensional array.
          Instead, it loops through the rows, which are themselves one-dimensional arrays.*/
        //Print "1 3 2 4 8 6"
        for (int[] eachRow : twoDimenArray) {
            for (int eachItem : eachRow) {
                System.out.print(eachItem + " ");
            }
        }
        /*Arrays.deepToString() prints a quick-and-dirty(应急的) list of the elements of a multidimensional array*/
        //Print "[[1, 3, 2], [4, 8, 6]]"
        System.out.println(Arrays.deepToString(twoDimenArray));
        /*"Arrays.deepToString() is illegal to one-dimensional array",
          as it requires the incoming parameter of "Object[]" type.*/
        //System.out.println(Arrays.deepToString(intArray8));

        /*Java has no multidimensional arrays at all, only one-dimensional arrays. A two-dimensional array is
          actually an one-dimensional array, whose every element is an one-dimensional array of "Object[]" type.
          So each row's length of a two dimensional-array can be different, this array is called a "Ragged Array".*/
        //It's legal to only assign the row's value of a two-dimensional array.
        int[][] raggedArray = new int[3][];
        /*It's illegal, as array initializer expected.*/
        //int[][] illegalRaggedArray = new int[][];
        /*It's illegal, as the row's value isn't assigned*/
        //int[][] illegalRaggedArray2 = new int[][3];
        int[] firstRow = raggedArray[0];
        raggedArray[0] = new int[]{1, 4};
        raggedArray[2] = new int[]{5, 7, 9};
        //Print "[[1, 4], null, [5, 7, 9]]"
        System.out.println(Arrays.deepToString(raggedArray));
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java数组常用的工具方法和字段(Java Array‘s common methods and fields) 的相关文章

随机推荐