I am trying to do this
List<String> stringList = new ArrayList<String>();
stringList.add("one");
stringList.add("two");
stringList.add("three");
(String[]) stringList.toArray();
why does this gives me class cast exception ?
Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
Better try this other method:
In that way the array returned is of the exact type that you need, no cast needed. The method you’re invoking returns an
Object[], not aString[]and the cast you’re trying to perform is invalid.