A final counter in a for loop?

后端 未结 6 890
滥情空心
滥情空心 2020-12-03 21:07

I have this code:

    List r = new ArrayList<>();
    for(int i = 0; i < 10; i++) {
        r.add(new Runnable() {

            @Ove         


        
6条回答
  •  日久生厌
    2020-12-03 21:58

    I'm afraid there's no other way than copy your counter to a second final variable and use that in your anonymous inner class. This is one of the "deficiencies" of Java around the topic of closures and an advertised advantage of sibling languages like Groovy.

提交回复
热议问题