Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Array ---- Re-initialise
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Aghil krishna
Array ---- Re-initialise
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


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


Advertisement
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:
Advertisements
 

 

 

DMCA.com