Convert List<Object> to String[] in Java
You have to loop through the list and fill your String[]
.
String[] array = new String[lst.size()];
int index = 0;
for (Object value : lst) {
array[index] = (String) value;
index++;
}
If the list would be of String
values, List then this would be as simple as calling lst.toArray(new String[0])
;
You could use toArray() to convert into an array of Objects followed by this method to convert the array of Objects into an array of Strings:
Object[] objectArray = lst.toArray();
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
Java 8 has the option of using streams like:
List<Object> lst = new ArrayList<>();
String[] strings = lst.stream().toArray(String[]::new);