Java implements a redefinition array similar to VB's ReDim

Source: Internet
Author: User

Code farm:
// Param objArr the expanded object of Array.
// Param newLength the length of the new Array
Public static Object getNewArr (Object objArr, int newLength ){
If (! ObjArr. getClass (). isArray () {// determine the type
Return null;
}
// Get the array's componentType
Class componentType = objArr. getClass (). getComponentType (); // obtain the type
// Get a newInstance of a Array object
Object newArray = Array. newInstance (componentType, newLength); // create an Array Object
// Copy the array
System. arraycopy (objArr, 0, newArray, 0, Array. getLength (objArr); // copy the original array data to the new array, where newLength must be greater than the length of the meta-objArr; otherwise, an error is returned.
Return newArray;
}
Share:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.