<%
ArrayList aList=(ArrayList)request.getAttribute("read");
String[] write =aList.toArray(new String[aList.size()]);
%>
在上面的代码中,我得到
incompatible types: Object[] cannot be converted to String[].
如果我只转换我会得到我想要的答案,我 google并得到了很多解决方案,但没有给我一个解决方案,帮助我找出这个问题。预先感谢您的回复。
最佳答案
问题在于 aList
的类型是raw type ArrayList
。这意味着所有泛型的痕迹都将从其中删除 - 甚至从泛型方法 toList
中删除。 .
修复起来很简单 - 只需使用 ArrayList<?>
相反:
ArrayList<?> aList = (ArrayList<?>)request.getAttribute("read");
原始类型通常是一个令人头疼的问题,并且实际上只是为了向后兼容而包含在内 - 尽可能避免它们。
关于java - jsp中Object[]无法转换为String[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728344/