EN
Java - Error: java: incompatible types: java.lang.Object[] cannot be converted to java.lang.String[] - convert ArrayList to String[]
1
answers
0
points
I tried converting an ArrayList to an array of strings (String []) and got this error:
java: incompatible types: java.lang.Object[] cannot be converted to java.lang.String[]
My code:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("x");
arrayList.add("y");
String[] strings = arrayList.toArray(); // this line throw an error
}
}
1 answer
0
points
I found a solution - It needs to change
String[] strings = arrayList.toArray();
to
String[] text = arrayList.toArray(String[]::new)
I also wrote an article about it - Java - convert ArrayList to array[].
0 comments
Add comment