
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; } 这段代码中为什么会有 ((Object)newType == (Object)Object[].class) 的判断,难道直接 (T[]) Array.newInstance(newType.getComponentType(), newLength); 不能处理前面那种情况吗?我单独把这个函数拿出来,去掉三元表达式前面的判断,然后调用 copyOf(intArr, 5, Object[].class) 试了一下,貌似也可以啊。
1 aristotll 2018-02-28 23:40:32 +08:00 应该是效率问题吧 反射慢吧 |
2 1stlulu 2018-02-28 23:40:35 +08:00 via Android |
3 aristotll 2018-02-28 23:42:21 +08:00 |