<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] # Arrays 這個工具類可以完成所有與數組有關的操作功能 ## asList方法,返回一個固定大小的List ~~~ @SafeVarargs public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } ~~~ 使用該方法可以返回一個固定大小的List,如: ~~~ List<String> stringList = Arrays.asList("Welcome", "To", "Java", "World!"); List<Integer> intList = Arrays.asList(1, 2, 3, 4); ~~~ ## binarySearch方法 **需要排好序的** binarySearch方法支持在整個數組中查找,如: ~~~ int index = Arrays.binarySearch(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 6); ~~~ 以及在某個區間范圍內查找, 如: ~~~ public static int binarySearch(int[] a, int fromIndex, int toIndex, int key) { rangeCheck(a.length, fromIndex, toIndex); return binarySearch0(a, fromIndex, toIndex, key); } int index = Arrays.binarySearch(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 1, 6, 6); ~~~ ## copyOf及copyOfRange方法,復制數組 注意:如果復制的是對象數組,那么只是對象的引用,而不是對象本身的拷貝 ~~~ String[] names2 = { "Eric", "John", "Alan", "Liz" }; //[Eric, John, Alan] String[] copy = Arrays.copyOf(names2, 3); //[Alan, Liz] String[] rangeCopy = Arrays.copyOfRange(names2, 2, names2.length); ~~~ ## sort方法 ~~~ String[] names = { "Liz", "John", "Eric", "Alan" }; //只排序前兩個 //[John, Liz, Eric, Alan] Arrays.sort(names, 0, 2); //全部排序 //[Alan, Eric, John, Liz] Arrays.sort(names); ~~~ 另外,Arrays的sort方法也可以結合比較器,完成更加復雜的排序。 ~~~ public static <T> void sort(T[] a, Comparator<? super T> c) { if (LegacyMergeSort.userRequested) legacyMergeSort(a, c); else TimSort.sort(a, c); } ~~~ ## toString方法,方便打印出數組內容 Arrays的toString方法可以方便我們打印出數組內容。 如: ~~~ String[] names = { "Liz", "John", "Eric", "Alan" }; Arrays.sort(names); System.out.println(Arrays.toString(names)); ~~~ 控制臺將打印出`[Alan, Eric, John, Liz] ` ## deepToString方法,打印二維數組的內容 如果需要打印二維數組的內容: ~~~ int[][] stuGrades = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } }; ~~~ 如果直接用 ~~~ System.out.println(Arrays.toString(stuGrades)); ~~~ 那么得到的結果類似于 ~~~ [[I@35ce36, [I@757aef, [I@d9f9c3]} ~~~ 這個時候得用 deepToString 方法才能得到正確的結果`[[80, 81, 82], [84, 85, 86], [87, 88, 89]]` ~~~ System.out.println(Arrays.deepToString(stuGrades)); ~~~ ## equals方法 使用Arrays.equals來比較1維數組是否相等。 **需要數組順序完全一致** ~~~ String[] names1 = { "Eric", "John", "Alan", "Liz" }; String[] names2 = { "Eric", "John", "Alan", "Liz" }; System.out.println(Arrays.equals(names1, names2)); ~~~ ## deepEquals方法,斷更加復雜的數組是否相等 Arrays.deepEquals能夠去判斷更加復雜的數組是否相等。 ~~~ int[][] stuGrades1 = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } }; int[][] stuGrades2 = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } }; System.out.println(Arrays.deepEquals(stuGrades1, stuGrades2)); ~~~ ## fill方法,填充數組 將指定的元素放在指定的數組中 ~~~ int[] array1 = new int[8]; Arrays.fill(array1, 1); //[1, 1, 1, 1, 1, 1, 1, 1] System.out.printl ~~~ ## parallelsort并行排序 ~~~ double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; Arrays.parallelSort(numbers);//對整個數組進行排序; Arrays.parallelSort(numbers); char[] chars = {'a', 'A', '4', 'F', 'D', 'P'}; Arrays.parallelSort(chars, 1, 3);//對部分數組進行排序,從chars[1]到chars[3]; Arrays.sort(chars, 1, 3); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看