There is no API for modifying the length of an array in Java, where I provide two functions to modify the length of the array: Arrayaddlength () and Arrayreducelength (). See Code in detail. [java] View Plaincopyprint?import Java.lang.reflect.Array; /** * Description:this class is used to adjust array length. * @author E421083458 *&nbs p;*/ public class Arraytest { /** * @param args */ public static void Main (String [] args) { int a[]= new int[]{0,1,2,3,4,5}; int b[]= new int[]{0,1,2,3,4,5}; a = (int[]) arrayaddlength ( a,2); b = (int[]) arrayreducelength (b,2); //out Print Array lenght system.out.println (a.length); for (int i=0;i<a.length;i++) { system.out.print (a[i]); } system.out.println (); system.out.println (b.length); for (int i=0;i<b.length;i++) { system.out.print (B[i]), } } /** * Description: Array add length * @param oldarray * @param addlength * @return object */ public Static Object Arrayaddlength (Object Oldarray,int addlength) { class c = oldarray.getclass (); if (!c.isarray () ) return null; class ComponentType = C.getcomponenttype (); int length = Array.getlength (Oldarray); int newlength = length + addlength; object NewArray = array.newinstance (componenttype,newlength); system.arraycopy (oldarray,0,newarray,0,length); return NewArray; } /** * description:array reduce lenght * @param oldarray * @param reducelength * @ Return object */ public Static object Arrayreducelength (Object Oldarray,int reducelength) { class c = old Array.getclass (); if (!c.isarray ()) return null; class ComponentType = C.getcomponenttype (); int length = Array.getlength (Oldarray); int newlength = length-reducelength; object NewArray = array.newinstance (componenttype,newlength); system.arraycopy (oldarray,0,newarray,0,newlength); return NewArray; &nBSP;} }
Java modifies array length