+ 2

array initialization

How to initialize an array in java if its size is unknown?

16th Jan 2017, 3:02 PM
Ijas Ahammed
Ijas Ahammed - avatar
1 Odpowiedź
+ 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();
16th Jan 2017, 3:15 PM
John Antonius