Array ---- Re-initialise

Array ---- Re-initialise

How can i re-initialise the initial capacity of an Array when it's exceed the old capacity ...?

View Answers

November 17, 2010 at 1:31 PM

Hello Friend,

Try the following code:

class ReinitializeArray{

public static int[] resizeArray(int[] arrayToResize,int newsize) {
    int[] newArray = new int[newsize];
    System.arraycopy(arrayToResize, 0,newArray, 0, arrayToResize.length);
    return newArray;
}
public static void main (String[] args) {
   int[] arr = {1,2};
   arr = (int[])resizeArray(arr,5);
   arr[2] = 3;
   arr[3] = 4;
   arr[4] = 5;
   for (int i=0; i<arr.length; i++)
   System.out.println (arr[i]);
   }
}

Thanks









Related Tutorials/Questions & Answers:

Ads