Share very useful Java programs (key code) (iv)---dynamically change the size of the array

Source: Internet
Author: User

Original: Share very useful Java program (key Code) (iv)---dynamically change the size of the array

/** * Reallocates An array with a new size, and copies the contents * * of the old array to the new array.   * * @param oldarray the old array and to be reallocated.   * * @param newSize the new array size.   * * @return A new array with the same contents. * */private static object Resizearray (object oldarray, int newSize) {int oldsize = Java.lang.reflect.Array.getLeng      Th (Oldarray);      Class ElementType = Oldarray.getclass (). Getcomponenttype ();      Object NewArray = java.lang.reflect.Array.newInstance (elementtype,newsize);      int preservelength = Math.min (oldsize,newsize);      if (Preservelength > 0) system.arraycopy (oldarray,0,newarray,0,preservelength);   return newarray;   }//Test routine for Resizearray ().      public static void Main (string[] args) {int[] a = {-N-a};      A = (int[]) Resizearray (a,5);      A[3] = 4;      A[4] = 5;   for (int i=0; i<a.length; i++) System.out.println (A[i]); }

the code just implements the basic method, detailed processing also need you to coding oh >>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Share very useful Java programs (key code) (iv)---dynamically change the size of the array

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.