Skip to content

Instantly share code, notes, and snippets.

@Orange168
Created July 14, 2018 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orange168/53c89900f1eea640964b0a488aa17826 to your computer and use it in GitHub Desktop.
Save Orange168/53c89900f1eea640964b0a488aa17826 to your computer and use it in GitHub Desktop.
[不同类型数组复制] 数组复制#不同类型数组复制#Java#Array
public static Object goodCopyOf(Object a, int newLength)
{
Class cl = a.getClass();
if (!cl.isArray()) return null;
Class componentType = cl.getComponentType();
int length = Array.getLength(a);
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, Math.min(length, newLength));
return newArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment