+ 2
array initialization
How to initialize an array in java if its size is unknown?
1 Answer
+ 1
Java array initialization must always have number of elements.
Either stated explicitly, like:
String[] myArray = new String[1];
Or implicitly by enumerating the array elements, like:
String[] myArray = new String[] {"a", "b", "c"};
Data structure in Java for unknown length of data collection is usually List. Later on it can be converted to array.
Example:
List<String> myList = new ArrayList<>();
// several myList.add()
String[] myArray = (String[]) myList.toArray();