newArr[newArrLength - 1] = obj;
return newArr;
}
-
+
+ /**
+ * prepend <code>obj</code> to <code>array</code>
+ *
+ * Return array having only <code>obj</code> if <code>array</code> is <code>null</code>
+ *
+ * @param array array to add element
+ * @param obj new element to add
+ *
+ * @return added array
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T[]
+ prepend(
+ final T[] array,
+ final T obj
+ )
+ {
+ Class<?> compType = Object.class;
+ if ( null != array )
+ {
+ compType = array.getClass().getComponentType();
+ }
+ else if ( null != obj )
+ {
+ compType = obj.getClass();
+ }
+
+ final int newArrLength = size( array ) + 1;
+ final T[] newArr =
+ (T[]) Array.newInstance( compType, newArrLength );
+
+ if ( null != array )
+ {
+ System.arraycopy( array, 0, newArr, 1, array.length );
+ }
+ newArr[0] = obj;
+ return newArr;
+ }
+
/**
* remove elements whose index is between <code>startIndex</code> and <code>endIndex</code>
*