栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java库常用的方法及其复杂度分析Common Java APIs and complexity analysis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java库常用的方法及其复杂度分析Common Java APIs and complexity analysis

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

1.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[] = 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[]{};
        
        //int[] emptyIntArray2 = new int[];
        
        System.out.println(Arrays.toString(emptyIntArray) + " " + emptyIntArray.length);
        
        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};
        
        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];
        
        //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]);
        }
        
        for (int j : intArray) {
            System.out.println(j);
        }
        //Print "[I@45ee12a7"
        System.out.println(intArray);
        
        //Print "[1, 2,3]"
        System.out.println(Arrays.toString(intArray));

        
        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};
        
        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);
        
        System.out.println(Arrays.equals(intArray8, intArray9));

        //Multi-dimensional Arrays
        int[][] twoDimenArray = {
                {1, 3, 2},
                {4, 8, 6}
        };
        
        //Print "1 3 2 4 8 6"
        for (int[] eachRow : twoDimenArray) {
            for (int eachItem : eachRow) {
                System.out.print(eachItem + " ");
            }
        }
        
        //Print "[[1, 3, 2], [4, 8, 6]]"
        System.out.println(Arrays.deepToString(twoDimenArray));
        
        //System.out.println(Arrays.deepToString(intArray8));

        
        //It's legal to only assign the row's value of a two-dimensional array.
        int[][] raggedArray = new int[3][];
        
        //int[][] illegalRaggedArray = new int[][];
        
        //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));
    }

}
2 Java Binary Operator / and% 2.1 Java Binary Operator /

According to Oracle Java SE Specifications - 15.17.2. Division Operator /. The binary operator / performs division, producing the quotient of its operands. The left-hand operand is the dividend and the right-hand operand is the divisor.

The quotient produced by the divisible symbol / is the result of normal division followed by subtracting the decimal part, rather than the result of rounding. The positive and negative sign of the quotient is determined by both signs of the dividend and the divisor.

整除符号/产生的最终数值是普通除法后再去除小数位的数值,而不是四舍五入的结果。整除符号/产生的最终数值的正负符号由除数和被除数共同决定。

System.out.println(4 / 3);   //Output 1
System.out.println(5 / 3);   //Output 1
System.out.println(-4 / 3);  //Output -1
System.out.println(-5 / 3);  //Output -1
System.out.println(4 / -3);  //Output -1
System.out.println(5 / -3);  //Output -1
System.out.println(-4 / -3); //Output 1
System.out.println(-5 / -3); //Output 1

magnitude /ˈmæɡnɪtuːd/ n量级,程度(在下文中可以近似理解为绝对值)

Absolute value, Measure of the magnitude of a real number, complex number, or vector.

There is one special case that does not satisfy this rule: if the dividend is the negative integer of largest possible magnitude for its type, and the divisor is -1, then integer overflow occurs and the result is equal to the dividend. Despite the overflow, no exception is thrown in this case.

System.out.println(Integer.MIN_VALUE/-1); //Output -2147483648
2.2 Java Binary Operator %

According to Oracle Java SE Specifications - 15.17.3. Remainder Operator %. The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.

It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive.

模运算符号%产生的最终数值是被模数和模数的绝对值取模后再复用模数正负号的数值,即其运算结果的符号始终和被模数的符号一致。

System.out.println(4 % 3); //Output 1
System.out.println(5 % 3); //Output 2
System.out.println(-4 % 3); //Output -1
System.out.println(-5 % 3); //Output -2
System.out.println(4 % -3); //Output 1
System.out.println(5 % -3); //Output 2
System.out.println(-4 % -3); //Output -1
System.out.println(-5 % -3); //Output -2

References:

CSDN-LeetCode和牛客网的编程题中常用的Java方法和API
简书-java 整数除法

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847246.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号