Skip to content

Instantly share code, notes, and snippets.

@erictroebs
Last active June 24, 2021 16:11
Show Gist options
  • Save erictroebs/4f9f3c611cddb903b08a658605f47ec6 to your computer and use it in GitHub Desktop.
Save erictroebs/4f9f3c611cddb903b08a658605f47ec6 to your computer and use it in GitHub Desktop.
ClassCastException when converting generic lists to arrays in Java
import java.util.Arrays;
import java.util.LinkedList;
public class TestMenge {
LinkedList<String> StringList = new LinkedList<>();
public String[] getElements() {
return (String[]) StringList.toArray();
}
public static void main(String[] args) {
System.out.println(Runtime.version().toString());
System.out.flush();
TestMenge test = new TestMenge();
String[] StringArray = test.getElements();
System.out.println(Arrays.toString(StringArray));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment