📄 arrays.java
字号:
/* Arrays.java -- Utility class with methods to operate on arrays Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.util;import java.io.Serializable;import java.lang.reflect.Array;/** * This class contains various static utility methods performing operations on * arrays, and a method to provide a List "view" of an array to facilitate * using arrays with Collection-based APIs. All methods throw a * {@link NullPointerException} if the parameter array is null. * <p> * * Implementations may use their own algorithms, but must obey the general * properties; for example, the sort must be stable and n*log(n) complexity. * Sun's implementation of sort, and therefore ours, is a tuned quicksort, * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort * Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 * (November 1993). This algorithm offers n*log(n) performance on many data * sets that cause other quicksorts to degrade to quadratic performance. * * @author Original author unknown * @author Bryce McKinlay * @author Eric Blake <ebb9@email.byu.edu> * @see Comparable * @see Comparator * @since 1.2 * @status updated to 1.4 */public class Arrays{ /** * This class is non-instantiable. */ private Arrays() { }// binarySearch /** * Perform a binary search of a byte array for a key. The array must be * sorted (as by the sort() method) - if it is not, the behaviour of this * method is undefined, and may be an infinite loop. If the array contains * the key more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. */ public static int binarySearch(byte[] a, byte key) { int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final byte d = a[mid]; if (d == key) return mid; else if (d > key) hi = mid - 1; else // This gets the insertion point right on the last loop. low = ++mid; } return -mid - 1; } /** * Perform a binary search of a char array for a key. The array must be * sorted (as by the sort() method) - if it is not, the behaviour of this * method is undefined, and may be an infinite loop. If the array contains * the key more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. */ public static int binarySearch(char[] a, char key) { int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final char d = a[mid]; if (d == key) return mid; else if (d > key) hi = mid - 1; else // This gets the insertion point right on the last loop. low = ++mid; } return -mid - 1; } /** * Perform a binary search of a short array for a key. The array must be * sorted (as by the sort() method) - if it is not, the behaviour of this * method is undefined, and may be an infinite loop. If the array contains * the key more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. */ public static int binarySearch(short[] a, short key) { int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final short d = a[mid]; if (d == key) return mid; else if (d > key) hi = mid - 1; else // This gets the insertion point right on the last loop. low = ++mid; } return -mid - 1; } /** * Perform a binary search of an int array for a key. The array must be * sorted (as by the sort() method) - if it is not, the behaviour of this * method is undefined, and may be an infinite loop. If the array contains * the key more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. */ public static int binarySearch(int[] a, int key) { int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final int d = a[mid]; if (d == key) return mid; else if (d > key) hi = mid - 1; else // This gets the insertion point right on the last loop. low = ++mid; } return -mid - 1; } /** * Perform a binary search of a long array for a key. The array must be * sorted (as by the sort() method) - if it is not, the behaviour of this * method is undefined, and may be an infinite loop. If the array contains * the key more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. */ public static int binarySearch(long[] a, long key) { int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final long d = a[mid]; if (d == key) return mid; else if (d > key) hi = mid - 1; else // This gets the insertion point right on the last loop. low = ++mid; } return -mid - 1; } /** * Perform a binary search of a float array for a key. The array must be * sorted (as by the sort() method) - if it is not, the behaviour of this * method is undefined, and may be an infinite loop. If the array contains * the key more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. */ public static int binarySearch(float[] a, float key) { // Must use Float.compare to take into account NaN, +-0. int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final int r = Float.compare(a[mid], key); if (r == 0) return mid; else if (r > 0) hi = mid - 1; else // This gets the insertion point right on the last loop low = ++mid; } return -mid - 1; } /** * Perform a binary search of a double array for a key. The array must be * sorted (as by the sort() method) - if it is not, the behaviour of this * method is undefined, and may be an infinite loop. If the array contains * the key more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. */ public static int binarySearch(double[] a, double key) { // Must use Double.compare to take into account NaN, +-0. int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final int r = Double.compare(a[mid], key); if (r == 0) return mid; else if (r > 0) hi = mid - 1; else // This gets the insertion point right on the last loop low = ++mid; } return -mid - 1; } /** * Perform a binary search of an Object array for a key, using the natural * ordering of the elements. The array must be sorted (as by the sort() * method) - if it is not, the behaviour of this method is undefined, and may * be an infinite loop. Further, the key must be comparable with every item * in the array. If the array contains the key more than once, any one of * them may be found. Note: although the specification allows for an infinite * loop if the array is unsorted, it will not happen in this (JCL) * implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. * @throws ClassCastException if key could not be compared with one of the * elements of a * @throws NullPointerException if a null element in a is compared */ public static int binarySearch(Object[] a, Object key) { return binarySearch(a, key, null); } /** * Perform a binary search of an Object array for a key, using a supplied * Comparator. The array must be sorted (as by the sort() method with the * same Comparator) - if it is not, the behaviour of this method is * undefined, and may be an infinite loop. Further, the key must be * comparable with every item in the array. If the array contains the key * more than once, any one of them may be found. Note: although the * specification allows for an infinite loop if the array is unsorted, it * will not happen in this (JCL) implementation. * * @param a the array to search (must be sorted) * @param key the value to search for * @param c the comparator by which the array is sorted; or null to * use the elements' natural order * @return the index at which the key was found, or -n-1 if it was not * found, where n is the index of the first value higher than key or * a.length if there is no such value. * @throws ClassCastException if key could not be compared with one of the * elements of a * @throws NullPointerException if a null element is compared with natural * ordering (only possible when c is null) */ public static int binarySearch(Object[] a, Object key, Comparator c) { int low = 0; int hi = a.length - 1; int mid = 0; while (low <= hi) { mid = (low + hi) >> 1; final int d = Collections.compare(key, a[mid], c); if (d == 0) return mid; else if (d < 0) hi = mid - 1; else // This gets the insertion point right on the last loop low = ++mid; } return -mid - 1; }// equals /** * Compare two boolean arrays for equality. * * @param a1 the first array to compare * @param a2 the second array to compare * @return true if a1 and a2 are both null, or if a2 is of the same length * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i] */ public static boolean equals(boolean[] a1, boolean[] a2) { // Quick test which saves comparing elements of the same array, and also // catches the case that both are null. if (a1 == a2) return true; try { // If they're the same length, test each element if (a1.length == a2.length) { int i = a1.length; while (--i >= 0) if (a1[i] != a2[i]) return false; return true; } } catch (NullPointerException e) { // If one is null, we get a harmless NullPointerException } return false; } /** * Compare two byte arrays for equality. * * @param a1 the first array to compare * @param a2 the second array to compare * @return true if a1 and a2 are both null, or if a2 is of the same length * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i] */ public static boolean equals(byte[] a1, byte[] a2) { // Quick test which saves comparing elements of the same array, and also // catches the case that both are null. if (a1 == a2) return true; try { // If they're the same length, test each element if (a1.length == a2.length) { int i = a1.length; while (--i >= 0) if (a1[i] != a2[i]) return false; return true; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -