简体   繁体   English

Enumeration<> 枚举差异

[英]Enumeration<> Enumeration difference

I'm a biginner in JSP and I'm confused about the difference between Enumeration and Enumeration<type> I'm learning with this Korean book and the example source in it says Enumeration with the eclipse neon version it doesn't work.我是 JSP 的大人物,我对EnumerationEnumeration<type>之间的区别感到困惑我正在学习这本韩文书籍,其中的示例源说Enumeration与 eclipse neon 版本不起作用。 It works only when it write Enumeration<String> .它仅在写入Enumeration<String> Can someone tell me the difference?有人能告诉我有什么区别吗?

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; 

charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>헤더 목록 출력</title>
</head>
<body>
<% 
    Enumeration<String> headerEnum = request.getHeaderNames();
    while(headerEnum.hasMoreElements()){
        String headerName = (String)headerEnum.nextElement();
        String headerValue = request.getHeader(headerName);

%>
<%=headerName %> = <%=headerValue %> <br>
<%
    }
%>

</body>
</html>

Just give it a look at the Enumeration documentation .只需查看枚举文档即可 Also review the generic types documentation .另请查看泛型类型文档

By using Enumeration you are using Enumeration<Object> as it is the default.通过使用Enumeration您将使用Enumeration<Object>因为它是默认设置。 What this <Object> does is just indicate the Enumeration class that in that particular instance, the type that it calls E (in the Enumeration Documentation) will be resolved to Object.这个<Object>所做的只是指示枚举类,在该特定实例中,它调用 E(在枚举文档中)的类型将被解析为 Object。 By using <String> happens the same: the type called E will be resolved to String.通过使用<String>会发生相同的情况:称为 E 的类型将被解析为 String。

If you check the nextElement() signature it returns E. So, by using Enumeration or Enumeration<Object> that method will return Object and you will need the cast you did:如果您检查nextElement() 签名,它将返回 E。因此,通过使用EnumerationEnumeration<Object>该方法将返回 Object 并且您将需要您所做的强制转换:

 String headerName = (String)headerEnum.nextElement();

By using Enumeration<String> the method will return an String, so you can directly do this:通过使用Enumeration<String>该方法将返回一个字符串,因此您可以直接执行以下操作:

 String headerName = headerEnum.nextElement();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM