2013年9月6日星期五

Wanted to write a generic NullOrEmpty, I do not know whether it feasible ?

Wrote in such a way

public static boolean arrayNullOrEmpty(byte[] objs)
{
    return objs == null || objs.length == 0;
}


now want to use generics, is this method can be int [], double [] a general purpose , how to write
------ Solution ---------- ----------------------------------
overload
------ Solution - ------------------------------------------

public static <T> boolean arrayNullOrEmpty(T[] array) {
return array == null || array.length == 0;
}

public static boolean arrayNullOrEmpty(Object array) {
if (array == null) {
return true;
}

if (!array.getClass().isArray()) {
throw new IllegalArgumentException("{" + array + "} is not an array!");
}

return Array.getLength(array) == 0;
}

------ Solution ------------------------------------------ -
above that can deal with an array of objects , the following take-
------ For reference only ---------------------- -----------------
thxxxxxxxxxxxx

没有评论:

发表评论