Once you create an array, java doesn't provide the function to change its size (although you can, of course, change an individual array element). So we can write our own function to expand the array size.
This code shows how to expand array from size it was initialized with to something else.
String[] array = new String[5];
...
array = expand(array, 10);
//func defn
private String[] expand(String[] array, int size) {
String[] temp = new String[size];
System.arraycopy(array, 0, temp, 0, array.length);
for(int j = array.length; j < size; j++)
temp[j] = "";
return temp;
}
Note : If you frequently need to expand the size of an array while a program is running, you should use a different data structure called an array list.See here for array list.
No comments:
Post a Comment