Sure, here’s the translation: Java switch case中的break方法

huangapple go评论142阅读模式
英文:

Java switch case breaks method

问题

我有一个简单的 switch case和一个简单的方法

问题是对于 switch case 中的某些字符串调用相同的方法时该方法返回不同的结果

case: "list.all" 调用 listAll() 方法
导致以下输出

    listAll - 开始
    处理器process.listAll:06.200369.0
    处理器process.listAll:08.200388.0
    listAll - 结束


case: "list" 调用 listAll() 方法与之前相同的方法
但现在结果是

    listAll - 开始
    listAll - 结束

我不太清楚为什么 "switch" 影响方法的结果
有人能解释一下吗

@Override
public void process(String action, String v) {

    switch(action)
    {
        case ("delete"):
            this.delete(action, v);
            break;
        case ("list"):
            this.listAll();
            break;
        case ("list.all"):
            this.listAll();
            break;
        default:
            this.upsert(action,v);
    } // end switch

    context.commit();
} // end process

public void listAll() {
    System.out.println("listAll - 开始");
    KeyValueIterator<String, WrapperMinMax> iter = this.kvStore.all();
    while (iter.hasNext()) {
        KeyValue<String, WrapperMinMax> entry = iter.next();
        System.out.printf("处理器:process.listAll 键:%s,值:%s\n", entry.key.toString(), entry.value.getMax());
    }
    iter.close();
    System.out.println("listAll - 结束");
}
英文:

I have a simple switch case. And a simple method.

The problem is, for some strings in the switch case, while calling the same method.
The method returns a diffrent result.

case: "list.all" is calling listALL()
results in the follwing output

listAll - start
processor: process.listAll key: :06.2003, value: 69.0
processor: process.listAll key: :08.2003, value: 88.0
listAll - end

case: "list" is calling listALL() .. the same method as before
but now the result is

listAll - start
listAll - end

Not clear to me why the "switch" impacts the result of the method.
Can someone explain this please?

  @Override
public void process(String action, String v) {
switch(action)
{
case (&quot;delete&quot;):
this.delete(action, v);
break;
case (&quot;list&quot;):
this.listAll();
break;
case (&quot;list.all&quot;):
this.listAll();
break;
default:
this.upsert(action,v);
} // end switch
context.commit();
} // end process
public void listAll() {
System.out.println(&quot;listAll - start&quot;);
KeyValueIterator&lt;String, WrapperMinMax&gt; iter = this.kvStore.all();
while (iter.hasNext()) {
KeyValue&lt;String, WrapperMinMax&gt; entry = iter.next();
System.out.printf(&quot;processor: process.listAll key: :%s, value: %s\n&quot;, entry.key.toString(), entry.value.getMax());
}
iter.close();
System.out.println(&quot;listAll - end&quot;);
}

答案1

得分: 3

因为在这里 this.kvStore.all();,你没有创建一个新的迭代器,而是重用了一个已经超出范围的迭代器。

因此,调用 while 循环后,一次性使用了所有的迭代,第二次运行时,iter.hasNext() 返回 false

英文:

I think because here this.kvStore.all(); you are not creating new iterator but reuse the one that has exceeded already.

Hence calling while loop once eats up all the iterations and the second run gets iter.hasNext() returning false.

huangapple
  • 本文由 发表于 2020年7月25日 02:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63079779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定