How to remove all elements in String array in java?
If example
is not final
then a simple reassignment would work:
example = new String[example.length];
This assumes you need the array to remain the same size. If that's not necessary then create an empty array:
example = new String[0];
If it is final
then you could null
out all the elements:
Arrays.fill( example, null );
- See: void Arrays#fill(Object[], Object)
- Consider using an
ArrayList
or similar collection
example = new String[example.length];
If you need dynamic collection, you should consider using one of java.util.Collection implementations that fits your problem. E.g. java.util.List.