src/share/classes/java/util/Arrays.java

Print this page




2544             T midVal = a[mid];
2545             int cmp = c.compare(midVal, key);
2546             if (cmp < 0)
2547                 low = mid + 1;
2548             else if (cmp > 0)
2549                 high = mid - 1;
2550             else
2551                 return mid; // key found
2552         }
2553         return -(low + 1);  // key not found.
2554     }
2555 
2556     // Equality Testing
2557 
2558     /**
2559      * Returns <tt>true</tt> if the two specified arrays of longs are
2560      * <i>equal</i> to one another.  Two arrays are considered equal if both
2561      * arrays contain the same number of elements, and all corresponding pairs
2562      * of elements in the two arrays are equal.  In other words, two arrays
2563      * are equal if they contain the same elements in the same order.  Also,
2564      * two array references are considered equal if both are <tt>null</tt>.<p>
2565      *
2566      * @param a one array to be tested for equality
2567      * @param a2 the other array to be tested for equality
2568      * @return <tt>true</tt> if the two arrays are equal
2569      */
2570     public static boolean equals(long[] a, long[] a2) {
2571         if (a==a2)
2572             return true;
2573         if (a==null || a2==null)
2574             return false;
2575 
2576         int length = a.length;
2577         if (a2.length != length)
2578             return false;
2579 
2580         for (int i=0; i<length; i++)
2581             if (a[i] != a2[i])
2582                 return false;
2583 
2584         return true;
2585     }
2586 
2587     /**
2588      * Returns <tt>true</tt> if the two specified arrays of ints are
2589      * <i>equal</i> to one another.  Two arrays are considered equal if both
2590      * arrays contain the same number of elements, and all corresponding pairs
2591      * of elements in the two arrays are equal.  In other words, two arrays
2592      * are equal if they contain the same elements in the same order.  Also,
2593      * two array references are considered equal if both are <tt>null</tt>.<p>
2594      *
2595      * @param a one array to be tested for equality
2596      * @param a2 the other array to be tested for equality
2597      * @return <tt>true</tt> if the two arrays are equal
2598      */
2599     public static boolean equals(int[] a, int[] a2) {
2600         if (a==a2)
2601             return true;
2602         if (a==null || a2==null)
2603             return false;
2604 
2605         int length = a.length;
2606         if (a2.length != length)
2607             return false;
2608 
2609         for (int i=0; i<length; i++)
2610             if (a[i] != a2[i])
2611                 return false;
2612 
2613         return true;
2614     }
2615 
2616     /**
2617      * Returns <tt>true</tt> if the two specified arrays of shorts are
2618      * <i>equal</i> to one another.  Two arrays are considered equal if both
2619      * arrays contain the same number of elements, and all corresponding pairs
2620      * of elements in the two arrays are equal.  In other words, two arrays
2621      * are equal if they contain the same elements in the same order.  Also,
2622      * two array references are considered equal if both are <tt>null</tt>.<p>
2623      *
2624      * @param a one array to be tested for equality
2625      * @param a2 the other array to be tested for equality
2626      * @return <tt>true</tt> if the two arrays are equal
2627      */
2628     public static boolean equals(short[] a, short a2[]) {
2629         if (a==a2)
2630             return true;
2631         if (a==null || a2==null)
2632             return false;
2633 
2634         int length = a.length;
2635         if (a2.length != length)
2636             return false;
2637 
2638         for (int i=0; i<length; i++)
2639             if (a[i] != a2[i])
2640                 return false;
2641 
2642         return true;
2643     }
2644 
2645     /**
2646      * Returns <tt>true</tt> if the two specified arrays of chars are
2647      * <i>equal</i> to one another.  Two arrays are considered equal if both
2648      * arrays contain the same number of elements, and all corresponding pairs
2649      * of elements in the two arrays are equal.  In other words, two arrays
2650      * are equal if they contain the same elements in the same order.  Also,
2651      * two array references are considered equal if both are <tt>null</tt>.<p>
2652      *
2653      * @param a one array to be tested for equality
2654      * @param a2 the other array to be tested for equality
2655      * @return <tt>true</tt> if the two arrays are equal
2656      */
2657     public static boolean equals(char[] a, char[] a2) {
2658         if (a==a2)
2659             return true;
2660         if (a==null || a2==null)
2661             return false;
2662 
2663         int length = a.length;
2664         if (a2.length != length)
2665             return false;
2666 
2667         for (int i=0; i<length; i++)
2668             if (a[i] != a2[i])
2669                 return false;
2670 
2671         return true;
2672     }
2673 
2674     /**
2675      * Returns <tt>true</tt> if the two specified arrays of bytes are
2676      * <i>equal</i> to one another.  Two arrays are considered equal if both
2677      * arrays contain the same number of elements, and all corresponding pairs
2678      * of elements in the two arrays are equal.  In other words, two arrays
2679      * are equal if they contain the same elements in the same order.  Also,
2680      * two array references are considered equal if both are <tt>null</tt>.<p>
2681      *
2682      * @param a one array to be tested for equality
2683      * @param a2 the other array to be tested for equality
2684      * @return <tt>true</tt> if the two arrays are equal
2685      */
2686     public static boolean equals(byte[] a, byte[] a2) {
2687         if (a==a2)
2688             return true;
2689         if (a==null || a2==null)
2690             return false;
2691 
2692         int length = a.length;
2693         if (a2.length != length)
2694             return false;
2695 
2696         for (int i=0; i<length; i++)
2697             if (a[i] != a2[i])
2698                 return false;
2699 
2700         return true;
2701     }
2702 
2703     /**
2704      * Returns <tt>true</tt> if the two specified arrays of booleans are
2705      * <i>equal</i> to one another.  Two arrays are considered equal if both
2706      * arrays contain the same number of elements, and all corresponding pairs
2707      * of elements in the two arrays are equal.  In other words, two arrays
2708      * are equal if they contain the same elements in the same order.  Also,
2709      * two array references are considered equal if both are <tt>null</tt>.<p>
2710      *
2711      * @param a one array to be tested for equality
2712      * @param a2 the other array to be tested for equality
2713      * @return <tt>true</tt> if the two arrays are equal
2714      */
2715     public static boolean equals(boolean[] a, boolean[] a2) {
2716         if (a==a2)
2717             return true;
2718         if (a==null || a2==null)
2719             return false;
2720 
2721         int length = a.length;
2722         if (a2.length != length)
2723             return false;
2724 
2725         for (int i=0; i<length; i++)
2726             if (a[i] != a2[i])
2727                 return false;
2728 
2729         return true;
2730     }
2731 
2732     /**
2733      * Returns <tt>true</tt> if the two specified arrays of doubles are
2734      * <i>equal</i> to one another.  Two arrays are considered equal if both
2735      * arrays contain the same number of elements, and all corresponding pairs
2736      * of elements in the two arrays are equal.  In other words, two arrays
2737      * are equal if they contain the same elements in the same order.  Also,
2738      * two array references are considered equal if both are <tt>null</tt>.<p>
2739      *
2740      * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
2741      * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
2742      * (Unlike the <tt>==</tt> operator, this method considers
2743      * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
2744      *
2745      * @param a one array to be tested for equality
2746      * @param a2 the other array to be tested for equality
2747      * @return <tt>true</tt> if the two arrays are equal
2748      * @see Double#equals(Object)
2749      */
2750     public static boolean equals(double[] a, double[] a2) {
2751         if (a==a2)
2752             return true;
2753         if (a==null || a2==null)
2754             return false;
2755 
2756         int length = a.length;
2757         if (a2.length != length)
2758             return false;
2759 
2760         for (int i=0; i<length; i++)
2761             if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
2762                 return false;
2763 
2764         return true;
2765     }
2766 
2767     /**
2768      * Returns <tt>true</tt> if the two specified arrays of floats are
2769      * <i>equal</i> to one another.  Two arrays are considered equal if both
2770      * arrays contain the same number of elements, and all corresponding pairs
2771      * of elements in the two arrays are equal.  In other words, two arrays
2772      * are equal if they contain the same elements in the same order.  Also,
2773      * two array references are considered equal if both are <tt>null</tt>.<p>
2774      *
2775      * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
2776      * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
2777      * (Unlike the <tt>==</tt> operator, this method considers
2778      * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
2779      *
2780      * @param a one array to be tested for equality
2781      * @param a2 the other array to be tested for equality
2782      * @return <tt>true</tt> if the two arrays are equal
2783      * @see Float#equals(Object)
2784      */
2785     public static boolean equals(float[] a, float[] a2) {
2786         if (a==a2)
2787             return true;
2788         if (a==null || a2==null)
2789             return false;
2790 
2791         int length = a.length;
2792         if (a2.length != length)
2793             return false;
2794 
2795         for (int i=0; i<length; i++)
2796             if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
2797                 return false;
2798 
2799         return true;
2800     }
2801 
2802     /**
2803      * Returns <tt>true</tt> if the two specified arrays of Objects are
2804      * <i>equal</i> to one another.  The two arrays are considered equal if
2805      * both arrays contain the same number of elements, and all corresponding
2806      * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
2807      * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
2808      * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
2809      * they contain the same elements in the same order.  Also, two array
2810      * references are considered equal if both are <tt>null</tt>.<p>
2811      *
2812      * @param a one array to be tested for equality
2813      * @param a2 the other array to be tested for equality
2814      * @return <tt>true</tt> if the two arrays are equal
2815      */
2816     public static boolean equals(Object[] a, Object[] a2) {
2817         if (a==a2)
2818             return true;
2819         if (a==null || a2==null)
2820             return false;
2821 
2822         int length = a.length;
2823         if (a2.length != length)
2824             return false;
2825 
2826         for (int i=0; i<length; i++) {
2827             Object o1 = a[i];
2828             Object o2 = a2[i];
2829             if (!(o1==null ? o2==null : o1.equals(o2)))
2830                 return false;




2544             T midVal = a[mid];
2545             int cmp = c.compare(midVal, key);
2546             if (cmp < 0)
2547                 low = mid + 1;
2548             else if (cmp > 0)
2549                 high = mid - 1;
2550             else
2551                 return mid; // key found
2552         }
2553         return -(low + 1);  // key not found.
2554     }
2555 
2556     // Equality Testing
2557 
2558     /**
2559      * Returns <tt>true</tt> if the two specified arrays of longs are
2560      * <i>equal</i> to one another.  Two arrays are considered equal if both
2561      * arrays contain the same number of elements, and all corresponding pairs
2562      * of elements in the two arrays are equal.  In other words, two arrays
2563      * are equal if they contain the same elements in the same order.  Also,
2564      * two array references are considered equal if both are <tt>null</tt>.
2565      *
2566      * @param a one array to be tested for equality
2567      * @param a2 the other array to be tested for equality
2568      * @return <tt>true</tt> if the two arrays are equal
2569      */
2570     public static boolean equals(long[] a, long[] a2) {
2571         if (a==a2)
2572             return true;
2573         if (a==null || a2==null)
2574             return false;
2575 
2576         int length = a.length;
2577         if (a2.length != length)
2578             return false;
2579 
2580         for (int i=0; i<length; i++)
2581             if (a[i] != a2[i])
2582                 return false;
2583 
2584         return true;
2585     }
2586 
2587     /**
2588      * Returns <tt>true</tt> if the two specified arrays of ints are
2589      * <i>equal</i> to one another.  Two arrays are considered equal if both
2590      * arrays contain the same number of elements, and all corresponding pairs
2591      * of elements in the two arrays are equal.  In other words, two arrays
2592      * are equal if they contain the same elements in the same order.  Also,
2593      * two array references are considered equal if both are <tt>null</tt>.
2594      *
2595      * @param a one array to be tested for equality
2596      * @param a2 the other array to be tested for equality
2597      * @return <tt>true</tt> if the two arrays are equal
2598      */
2599     public static boolean equals(int[] a, int[] a2) {
2600         if (a==a2)
2601             return true;
2602         if (a==null || a2==null)
2603             return false;
2604 
2605         int length = a.length;
2606         if (a2.length != length)
2607             return false;
2608 
2609         for (int i=0; i<length; i++)
2610             if (a[i] != a2[i])
2611                 return false;
2612 
2613         return true;
2614     }
2615 
2616     /**
2617      * Returns <tt>true</tt> if the two specified arrays of shorts are
2618      * <i>equal</i> to one another.  Two arrays are considered equal if both
2619      * arrays contain the same number of elements, and all corresponding pairs
2620      * of elements in the two arrays are equal.  In other words, two arrays
2621      * are equal if they contain the same elements in the same order.  Also,
2622      * two array references are considered equal if both are <tt>null</tt>.
2623      *
2624      * @param a one array to be tested for equality
2625      * @param a2 the other array to be tested for equality
2626      * @return <tt>true</tt> if the two arrays are equal
2627      */
2628     public static boolean equals(short[] a, short a2[]) {
2629         if (a==a2)
2630             return true;
2631         if (a==null || a2==null)
2632             return false;
2633 
2634         int length = a.length;
2635         if (a2.length != length)
2636             return false;
2637 
2638         for (int i=0; i<length; i++)
2639             if (a[i] != a2[i])
2640                 return false;
2641 
2642         return true;
2643     }
2644 
2645     /**
2646      * Returns <tt>true</tt> if the two specified arrays of chars are
2647      * <i>equal</i> to one another.  Two arrays are considered equal if both
2648      * arrays contain the same number of elements, and all corresponding pairs
2649      * of elements in the two arrays are equal.  In other words, two arrays
2650      * are equal if they contain the same elements in the same order.  Also,
2651      * two array references are considered equal if both are <tt>null</tt>.
2652      *
2653      * @param a one array to be tested for equality
2654      * @param a2 the other array to be tested for equality
2655      * @return <tt>true</tt> if the two arrays are equal
2656      */
2657     public static boolean equals(char[] a, char[] a2) {
2658         if (a==a2)
2659             return true;
2660         if (a==null || a2==null)
2661             return false;
2662 
2663         int length = a.length;
2664         if (a2.length != length)
2665             return false;
2666 
2667         for (int i=0; i<length; i++)
2668             if (a[i] != a2[i])
2669                 return false;
2670 
2671         return true;
2672     }
2673 
2674     /**
2675      * Returns <tt>true</tt> if the two specified arrays of bytes are
2676      * <i>equal</i> to one another.  Two arrays are considered equal if both
2677      * arrays contain the same number of elements, and all corresponding pairs
2678      * of elements in the two arrays are equal.  In other words, two arrays
2679      * are equal if they contain the same elements in the same order.  Also,
2680      * two array references are considered equal if both are <tt>null</tt>.
2681      *
2682      * @param a one array to be tested for equality
2683      * @param a2 the other array to be tested for equality
2684      * @return <tt>true</tt> if the two arrays are equal
2685      */
2686     public static boolean equals(byte[] a, byte[] a2) {
2687         if (a==a2)
2688             return true;
2689         if (a==null || a2==null)
2690             return false;
2691 
2692         int length = a.length;
2693         if (a2.length != length)
2694             return false;
2695 
2696         for (int i=0; i<length; i++)
2697             if (a[i] != a2[i])
2698                 return false;
2699 
2700         return true;
2701     }
2702 
2703     /**
2704      * Returns <tt>true</tt> if the two specified arrays of booleans are
2705      * <i>equal</i> to one another.  Two arrays are considered equal if both
2706      * arrays contain the same number of elements, and all corresponding pairs
2707      * of elements in the two arrays are equal.  In other words, two arrays
2708      * are equal if they contain the same elements in the same order.  Also,
2709      * two array references are considered equal if both are <tt>null</tt>.
2710      *
2711      * @param a one array to be tested for equality
2712      * @param a2 the other array to be tested for equality
2713      * @return <tt>true</tt> if the two arrays are equal
2714      */
2715     public static boolean equals(boolean[] a, boolean[] a2) {
2716         if (a==a2)
2717             return true;
2718         if (a==null || a2==null)
2719             return false;
2720 
2721         int length = a.length;
2722         if (a2.length != length)
2723             return false;
2724 
2725         for (int i=0; i<length; i++)
2726             if (a[i] != a2[i])
2727                 return false;
2728 
2729         return true;
2730     }
2731 
2732     /**
2733      * Returns <tt>true</tt> if the two specified arrays of doubles are
2734      * <i>equal</i> to one another.  Two arrays are considered equal if both
2735      * arrays contain the same number of elements, and all corresponding pairs
2736      * of elements in the two arrays are equal.  In other words, two arrays
2737      * are equal if they contain the same elements in the same order.  Also,
2738      * two array references are considered equal if both are <tt>null</tt>.
2739      *
2740      * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
2741      * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
2742      * (Unlike the <tt>==</tt> operator, this method considers
2743      * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
2744      *
2745      * @param a one array to be tested for equality
2746      * @param a2 the other array to be tested for equality
2747      * @return <tt>true</tt> if the two arrays are equal
2748      * @see Double#equals(Object)
2749      */
2750     public static boolean equals(double[] a, double[] a2) {
2751         if (a==a2)
2752             return true;
2753         if (a==null || a2==null)
2754             return false;
2755 
2756         int length = a.length;
2757         if (a2.length != length)
2758             return false;
2759 
2760         for (int i=0; i<length; i++)
2761             if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
2762                 return false;
2763 
2764         return true;
2765     }
2766 
2767     /**
2768      * Returns <tt>true</tt> if the two specified arrays of floats are
2769      * <i>equal</i> to one another.  Two arrays are considered equal if both
2770      * arrays contain the same number of elements, and all corresponding pairs
2771      * of elements in the two arrays are equal.  In other words, two arrays
2772      * are equal if they contain the same elements in the same order.  Also,
2773      * two array references are considered equal if both are <tt>null</tt>.
2774      *
2775      * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
2776      * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
2777      * (Unlike the <tt>==</tt> operator, this method considers
2778      * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
2779      *
2780      * @param a one array to be tested for equality
2781      * @param a2 the other array to be tested for equality
2782      * @return <tt>true</tt> if the two arrays are equal
2783      * @see Float#equals(Object)
2784      */
2785     public static boolean equals(float[] a, float[] a2) {
2786         if (a==a2)
2787             return true;
2788         if (a==null || a2==null)
2789             return false;
2790 
2791         int length = a.length;
2792         if (a2.length != length)
2793             return false;
2794 
2795         for (int i=0; i<length; i++)
2796             if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
2797                 return false;
2798 
2799         return true;
2800     }
2801 
2802     /**
2803      * Returns <tt>true</tt> if the two specified arrays of Objects are
2804      * <i>equal</i> to one another.  The two arrays are considered equal if
2805      * both arrays contain the same number of elements, and all corresponding
2806      * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
2807      * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
2808      * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
2809      * they contain the same elements in the same order.  Also, two array
2810      * references are considered equal if both are <tt>null</tt>.
2811      *
2812      * @param a one array to be tested for equality
2813      * @param a2 the other array to be tested for equality
2814      * @return <tt>true</tt> if the two arrays are equal
2815      */
2816     public static boolean equals(Object[] a, Object[] a2) {
2817         if (a==a2)
2818             return true;
2819         if (a==null || a2==null)
2820             return false;
2821 
2822         int length = a.length;
2823         if (a2.length != length)
2824             return false;
2825 
2826         for (int i=0; i<length; i++) {
2827             Object o1 = a[i];
2828             Object o2 = a2[i];
2829             if (!(o1==null ? o2==null : o1.equals(o2)))
2830                 return false;