
public class Test07 { public static void main(String[] args) { // TODO Auto-generated method stub String[] strs=arrayT(String[]::new,"chen","shao","xiu"); System.out.println(Arrays.toString(strs)); //[chen, shao, xiu] Integer[] ins=arrayT(Integer[]::new,1,2,3,6,5,4); System.out.println(Arrays.toString(ins)); //[1, 2, 3, 6, 5, 4] } //需要提供一个构造器表达式 @SafeVarargs public static <T> T[] arrayT(IntFunction<T[]> constr,T...ts){ if(ts==null) return null; int length=ts.length; T[] arrt=constr.apply(length); for(int i=0;i<length;i++){ arrt[i]=ts[i]; } return arrt; } }