Bhagat Singh Rawat wrote:Actually
Array.newInstance
returns Object Array, so it is unknown what the type is.
It returns Object since it can also be an int[] if the class is Integer.TYPE (the class to represent the int primitive).
Matt, sometimes you cannot avoid warnings like these. Your case is one of those cases; you know that the Object returned is an array of T so the cast is in fact safe. The compiler can't verify though so that's why the warning is there. You can use the SuppressWarnings("unchecked") annotation. Keep it as local as possible:
By the way, you can move the creation of result outside the if-statement. No matter what value count has, you always call "Array.newInstance(clazz, count);". If count happens to be 0 then you just hard code that 0.
Ok, there is actually the case where the total array size would exceed Integer.MAX_VALUE so count wraps around and gets negative; in that case you may want to set count to Integer.MAX_VALUE itself.