How to reuse a variable without initializing a new one in for loop in java?

前端 未结 1 1952
轻奢々
轻奢々 2021-01-14 08:30

I was writing a function in java which counts the number of characters after white spaces in a string. This problem might sound trivial to some of you.

publ         


        
相关标签:
1条回答
  • 2021-01-14 09:15

    You don't need to declare a new variable:

    for (; position<a.length; position++) count++;
    

    You can leave any field of the for loop blank.

    Or better yet, why not:

    count = a.length - position;
    
    0 讨论(0)
提交回复
热议问题