读书人

JAVA 遍历表单Iterator!解决方案

发布时间: 2012-08-10 12:19:33 作者: rapoo

JAVA 遍历表单Iterator!
JAVA 中用Enumeration遍历Form 表单代码如下:

Java code
    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html;charset=gb2312");        PrintWriter out = response.getWriter();                Enumeration <String>  paramNames = request.getParameterNames();        while(paramNames.hasMoreElements()){            out.print(paramNames.nextElement());        }。。。。
,用Enumeration是可以实现遍历表单,但是Enumeration 这个接口已经被Iterator所替代,小弟用Iterator遍历了,报错“java.lang.ClassCastException: java.util.Collections$1 cannot be cast to java.util.Iterator”,请给位指点下,将上面的代码用Iterator 来实现遍历!谢谢!


[解决办法]
你好,request.getParameterNames();里边的结构是Hashtable,不能将其强制转化为Collection类,而Iterator是集合Collection的迭代器,因此不能用Iterator迭代。应该用枚举迭代器Enumeractor,因此只能用枚举类型来迭代。

读书人网 >J2EE开发

热点推荐