Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java枚举中跳过元素_Java_Servlets_Httprequest - Fatal编程技术网

从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);
}
}