Created
July 14, 2018 02:24
-
-
Save Orange168/53c89900f1eea640964b0a488aa17826 to your computer and use it in GitHub Desktop.
[不同类型数组复制] 数组复制#不同类型数组复制#Java#Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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