Java checks whether an array check is an empty array to check if an array contains an element

Source: Internet
Author: User

    /**      * Determine whether the given object is an array:     * Either an object array or a primitive array.      @param obj the object to check      */     Public Static Boolean IsArray (Object obj) {        returnnull && obj.getclass (). IsArray ());    } 
    /**      * Determine whether the given array is empty:     * i.e. {@code  null} or of zero length.      @param  array the array to check     */public    static Boolean IsEmpty (object[] array) {        returnnull | | array.length = = 0);    }
    /*** Check Whether the given array contains the given element. * @paramarray the array to check@codenull}, * in which case the return value would always be {@codefalse}) * @paramelement The element to check for *@returnwhether the element has been found in the given array*/     Public Static Booleancontainselement (object[] array, Object element) {if(Array = =NULL) {            return false; }         for(Object arrayele:array) {if(Nullsafeequals (Arrayele, Element)) {return true; }        }        return false; }

Two objects are equal, including array of numbers

    /*** Determine if the given objects is equal, returning {@codetrue} * If both is {@codenull} or {@codefalse} If only one is * {@codenull}. * <p>compares arrays with {@codeArrays.equals}, performing an equality * check based on the array elements rather than the array reference. * @paramO1 First Object to compare *@paramO2 Second Object to compare *@returnwhether the given objects is equal *@seejava.util.arrays#equals*/     Public Static BooleanNullsafeequals (Object O1, Object O2) {if(O1 = =O2) {            return true; }        if(O1 = =NULL|| O2 = =NULL) {            return false; }        if(O1.equals (O2)) {return true; }        if(O1.getclass (). IsArray () &&O2.getclass (). IsArray ()) {            if(O1instanceofObject[] && O2instanceofobject[]) {                returnarrays.equals ((object[]) O1, (object[]) O2); }            if(O1instanceof Boolean[] && O2instanceof Boolean[]) {                returnArrays.equals ((Boolean[]) O1, (Boolean[]) O2); }            if(O1instanceof byte[] && O2instanceof byte[]) {                returnArrays.equals ((byte[]) O1, (byte[]) O2); }            if(O1instanceof Char[] && O2instanceof Char[]) {                returnArrays.equals ((Char[]) O1, (Char[]) O2); }            if(O1instanceof Double[] && O2instanceof Double[]) {                returnArrays.equals ((Double[]) O1, (Double[]) O2); }            if(O1instanceof float[] && O2instanceof float[]) {                returnArrays.equals ((float[]) O1, (float[]) O2); }            if(O1instanceof int[] && O2instanceof int[]) {                returnArrays.equals ((int[]) O1, (int[]) O2); }            if(O1instanceof Long[] && O2instanceof Long[]) {                returnArrays.equals ((Long[]) O1, (Long[]) O2); }            if(O1instanceof  Short[] && O2instanceof  Short[]) {                returnArrays.equals (( Short[]) O1, ( Short[]) O2); }        }        return false; }

Determines whether a string is included in the enumeration array (ignoring case)

    /*** Check whether the given array of enum constants contains a constant with the given name, * ignoring case WH     En determining a match. * @paramEnumValues the enum values to check, typically the product of a call to Myenum.values () *@paramconstant The constant name to find (must isn't be null or empty string) *@returnwhether the constant have been found in the given array*/     Public Static BooleanContainsconstant (enum<?>[] enumvalues, String constant) {        returnContainsconstant (EnumValues, Constant,false); }

Determines whether a string is contained in the enumeration array

    /*** Check whether the given array of enum constants contains a constant with the given name. * @paramEnumValues the enum values to check, typically the product of a call to Myenum.values () *@paramconstant The constant name to find (must isn't be null or empty string) *@paramCaseSensitive Whether case was significant in determining a match *@returnwhether the constant have been found in the given array*/     Public Static BooleanContainsconstant (enum<?>[] enumvalues, String constant,Booleancasesensitive) {         for(enum<?>candidate:enumvalues) {            if(casesensitive?)candidate.tostring (). Equals (constant): Candidate.tostring (). Equalsignorecase (c Onstant)) {return true; }        }        return false; }

    /*** Case insensitive alternative to {@linkenum#valueof (Class, String)}. * @param<E> The concrete Enum type *@paramEnumValues The array of all enums constants in question, usually per enum.values () *@paramconstant The constant to get the enum value of *@throwsIllegalArgumentException If the given constant is not found in the given array * of enum values. Use {@link#containsConstant (enum[], String)} as a guard to avoid this exception. */     Public Static<eextendsEnum<?>>E caseinsensitivevalueof (e[] enumvalues, String constant) { for(E candidate:enumvalues) {if(Candidate.tostring (). Equalsignorecase (constant)) {returncandidate; }        }        Throw Newillegalargumentexception (String.Format ("Constant [%s] does not exist in enum type%s", Constant, Enumvalues.getclass (). Getcomponenttype (). GetName ())); }

Java checks whether an array check is an empty array to check if an array contains an element

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.