Wednesday, May 18, 2011

Increase the size of an array

In Java there is no direct way to increase the size of an array after is is defined.
In this post I will show you how to do this easily.
Think you have following array. The size of this array is 1.

String[] arr = new String[1];

Now you can increase the size of your array like this.(replace the word "NEW_SIZE" with your new size)

arr=(String[]) Arrays.asList(arr).toArray(new String[NEW_SIZE]);

No comments:

Post a Comment