Wednesday, 19 September 2012

How to create or initialize Generic Type Array


  • solution 1





  • Limitation: It's not work if input array is null.

    public static <T> T[] subarray(T[] array, int startIndexInclusive, int endIndexExclusive) {
            if (array == null) {
                return null;
            }
    
            if (startIndexInclusive < 0) {
                startIndexInclusive = 0;
            }
            if (endIndexExclusive > array.length) {
                endIndexExclusive = array.length;
            }
    
            Class<?> type = array.getClass().getComponentType();
            int newSize = endIndexExclusive - startIndexInclusive;
            if (newSize < 0) {
                final T[] emptyArray = (T[]) Array.newInstance(type, 0);
                return emptyArray;
            }
    
            T[] subarray = (T[]) Array.newInstance(type, newSize);
            System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
            return subarray;
        }
    
  • solution 2
  • It's work if input array is null. But it's more verbose for invoking.
    public static <T> T[] subarray(T[] array, int startIndexInclusive, int endIndexExclusive, Class<T>> clazz) {
         T[] emptyArray = (T[]) Array.newInstance(clazz, 0);
    
            if (array == null) {
                return emptyArray;
            }
    
            if (startIndexInclusive < 0) {
                startIndexInclusive = 0;
            }
            if (endIndexExclusive > array.length) {
                endIndexExclusive = array.length;
            }
    
            if (newSize < 0) {
                return emptyArray;
            }
    
            T[] subarray = (T[]) Array.newInstance(clazz, newSize);
            System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
            return subarray;
        }
    

    No comments:

    Post a Comment