Other answers have already said how to do it right. For completeness in Java, each array has a fixed size (length), which is determined at creation and never changes. (An array also has a component type that never changes.)
So, you will need to create a new (larger) array when your old array is full, and copy the old content. Fortunately, the ArrayList class does this for you when its internal support array is full, so you can focus on the actual business task.
source share