从Java枚举中跳过元素
我想使用枚举跳过某个请求参数。我使用下面的代码,但它没有给我想要的结果。有人能告诉我如何从枚举中跳过一个元素,或者下面的代码有什么问题吗从Java枚举中跳过元素,java,servlets,httprequest,Java,Servlets,Httprequest,我想使用枚举跳过某个请求参数。我使用下面的代码,但它没有给我想要的结果。有人能告诉我如何从枚举中跳过一个元素,或者下面的代码有什么问题吗 for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) { if("James".equalsIgnoreCase(e.nextElement().toString())) { e.nextElement
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
if("James".equalsIgnoreCase(e.nextElement().toString())) {
e.nextElement();
continue;
} else {
list.add(e.nextElement().toString());
}
}
for(枚举e=request.getParameterNames();e.hasMoreElements();){
if(“James”.equalsIgnoreCase(例如nextElement().toString())){
e、 nextElement();
继续;
}否则{
添加(例如nextElement().toString());
}
}
您正在为跳过多个元素的每个循环调用nextElement()
多次。只需调用一次nextElement()
。类似于
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
String value = e.nextElement();
if(!"James".equalsIgnoreCase(value)) {
list.add(value);
}
}
for(枚举e=request.getParameterNames();e.hasMoreElements();){
字符串值=e.nextElement();
if(!“James”.equalsIgnoreCase(值)){
列表。添加(值);
}
}
您正在为跳过多个元素的每个循环调用nextElement()
多次。只需调用一次nextElement()
。类似于
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
String value = e.nextElement();
if(!"James".equalsIgnoreCase(value)) {
list.add(value);
}
}
for(枚举e=request.getParameterNames();e.hasMoreElements();){
字符串值=e.nextElement();
if(!“James”.equalsIgnoreCase(值)){
列表。添加(值);
}
}
问题是,您在if
中调用了e.nextElement()
两次。这将消耗两种元素
应首先将元素存储在字符串类型中,然后进行比较:-
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
String elem = e.nextElement();
if("James".equalsIgnoreCase(elem)) {
continue;
} else {
list.add(elem);
}
}
问题是,您在
if
中调用了e.nextElement()
两次。这将消耗两种元素
应首先将元素存储在字符串类型中,然后进行比较:-
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
String elem = e.nextElement();
if("James".equalsIgnoreCase(elem)) {
continue;
} else {
list.add(elem);
}
}
因为每次调用
nextElement()
时,都会从枚举中获取下一个元素。若枚举中并没有对象,您也可能会得到异常,您将尝试获取它
NoSuchElementException - if no more elements exist.
因此,只需更改代码并只调用一次nextElement()
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
String str= e.nextElement().toString();
if("James".equalsIgnoreCase(str)) {
continue;
} else {
list.add(str);
}
}
for(枚举e=request.getParameterNames();e.hasMoreElements();){
字符串str=e.nextElement().toString();
if(“James”。等信号情况(str)){
继续;
}否则{
添加(str);
}
}
因为每次调用nextElement()时,该方法都会从枚举中获取下一个元素。若枚举中并没有对象,您也可能会得到异常,您将尝试获取它
NoSuchElementException - if no more elements exist.
因此,只需更改代码并只调用一次nextElement()
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
String str= e.nextElement().toString();
if("James".equalsIgnoreCase(str)) {
continue;
} else {
list.add(str);
}
}
for(枚举e=request.getParameterNames();e.hasMoreElements();){
字符串str=e.nextElement().toString();
if(“James”。等信号情况(str)){
继续;
}否则{
添加(str);
}
}