Increase the length of object array

You need to cast the array to your type like this:

test = (TestClass[]) expand(test, newlength);

Resizing the array one element at a time will be slow. Usually the best idea is to double the size and keep the number of elements in the array in an int. Or as @Lexyth suggested, use ArrayList, it resizes automatically.

2 Likes