如何在 foreach 循环内递增计数

huangapple 未分类评论50阅读模式
英文:

How to increment count within foreach loop

问题

  1. public static void main(String[] args) {
  2. int count = 0;
  3. List<String> namesList = new ArrayList<>();
  4. namesList.add("gaurav");
  5. namesList.add("deepak");
  6. namesList.add("anit");
  7. namesList.add("garvit");
  8. namesList.add("satvir");
  9. namesList.add("lino");
  10. namesList.add("gogo");
  11. namesList.forEach(names -> {
  12. if (names.startsWith("g")) {
  13. count++;
  14. }
  15. });
  16. if (count > 1) {
  17. System.out.println("executed");
  18. }
  19. }

在增加计数时遇到以下错误:
在封闭范围中定义的局部变量 "count" 必须是 final 或者是 effectively final

英文:

Below is my code:

  1. public static void main(String[] args) {
  2. int count = 0;
  3. List&lt;String&gt; namesList = new ArrayList&lt;&gt;();
  4. namesList.add(&quot;gaurav&quot;);
  5. namesList.add(&quot;deepak&quot;);
  6. namesList.add(&quot;anit&quot;);
  7. namesList.add(&quot;garvit&quot;);
  8. namesList.add(&quot;satvir&quot;);
  9. namesList.add(&quot;lino&quot;);
  10. namesList.add(&quot;gogo&quot;);
  11. namesList.forEach(names -&gt; {
  12. if (names.startsWith(&quot;g&quot;)) {
  13. count++;
  14. }
  15. });
  16. if (count &gt; 1) {
  17. System.out.println(&quot;executed&quot;);
  18. }
  19. }

Getting the below error while incrementing count:
Local variable count defined in an enclosing scope must be final or effectively final

答案1

得分: 1

不要增加一个变量。

筛选计数

  1. long count = namesList.stream()
  2. .filter(name -> name.startsWith("g"))
  3. .count();

这样更易于阅读和理解,而且代码更简洁。

英文:

Don't increment a variable.

Stream, filter and count:

  1. long count = namesList.stream()
  2. .filter(name -&gt; name.startsWith(&quot;g&quot;))
  3. .count();

It's eaisier to read and understand, and it's less code.

答案2

得分: 0

使用 AtomicInteger 进行如下操作:

  1. AtomicInteger count = new AtomicInteger();
  2. namesList.forEach(names -> {
  3. if (names.startsWith("g")) {
  4. count.getAndIncrement();
  5. }
  6. });
  7. if (count.get() > 1) {
  8. System.out.println("executed");
  9. }

或者使用 Stream API 进行如下操作:

  1. long count = namesList.stream()
  2. .filter(name -> name.startsWith("g")) // 使用条件进行过滤
  3. .count(); // 然后计数
英文:

Use AtomicInteger for this

  1. AtomicInteger count = new AtomicInteger();
  2. namesList.forEach(names -&gt; {
  3. if (names.startsWith(&quot;g&quot;)) {
  4. count.getAndIncrement();
  5. }
  6. });
  7. if(count.get() &gt; 1) {
  8. System.out.println(&quot;executed&quot;);
  9. }

Or using Stream API

  1. long count = namesList.stream()
  2. .filter(name -&gt; name.startsWith(&quot;g&quot;)) // filter with condition
  3. .count(); // then count

答案3

得分: 0

你也可以使用流来实现这个。使用 filter() 来查找以 g 开头的名称,使用 findFirst() 来找到第一个。如果没有符合条件的,将返回 null

  1. String result = namesList
  2. .stream()
  3. .filter(name -> name.startsWith("g"))
  4. .findFirst()
  5. .orElse(null);
英文:

You can do that with a stream as well. Use filter() to find the names starting with g, use findFirst(), to find the first one. If there is none, this will return null.

  1. String result = namesList
  2. .stream()
  3. .filter(name -&gt; name.startsWith(&quot;g&quot;))
  4. .findFirst()
  5. .orElse(null);

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

发表评论

匿名网友

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

确定