toArray cast java.lang.ClassCastException

[toArray conversion stepping on the pit java.lang.ClassCastException]

  1. question
List<String> auditOptions = Lists.newArrayList();
//一系列灌数据操作
auditOption.add...
String[] options = (String[]) auditOptions.toArray();
报错信息

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String;
([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; [Ljava.lang.String;
is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @51081592)

2. Solve

a. After checking the information, I found out that the upward transformation does not need to be forced, the object type to be forced must be the same type or a subclass of the type that needs to be forced, so Object to String is not feasible

b. I went through toArray again and found that there is another generic method

3. Retry to resolve

Guess you like

Origin blog.csdn.net/Wis57/article/details/130005079