<%
ArrayList aList=(ArrayList)request.getAttribute("read");
String[] write =aList.toArray(new String[aList.size()]);
%>
in above code, i am getting
incompatible types: Object[] cannot be converted to String[].
if i cast only i will get the answer what i wish, i google and got lot of solution, but nothing gave me a solution, help me to find out of this problem. thanks in advance for your replies.
The problem is that the type of aList
is the raw type ArrayList
. That means all trace of generics is removed from it - even from the generic method toList
.
It's simple to fix though - just use ArrayList<?>
instead:
ArrayList<?> aList = (ArrayList<?>)request.getAttribute("read");
Raw types are a pain in the neck in general, and were really only included for backward compatibility - try to avoid them where possible.
See more on this question at Stackoverflow