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