【问题标题】:Can this while loop be made cleaner [closed]这个while循环可以变得更干净吗[关闭]
【发布时间】:2014-01-03 15:49:48
【问题描述】:

有没有办法让下面的 while 循环更加优化。尤其让我烦恼的是,我必须在 if 条件内部和外部重复代码(关闭缓冲区并返回一个值),我想就是否有更好/更面向性能的处理方式获得意见这样的代码。

while ((line = buf.readLine()) != null) {
  // Get a random line number
  if (currLine == lineToFetch) {
    quote = line;
    Log.v("LINE", line);
    randomQuote.close();
    buf.close();
    return quote;
  } else {
    currLine++;
  }
}

randomQuote.close();
buf.close();
return quote;

【问题讨论】:

  • 请发布您的其余方法。此外,在 codereview.stackexchange.com 上可能更合适
  • 我不这么认为,但是在java中没有什么是不可能的所以试试=0
  • 这个问题似乎属于Code Review
  • 谢谢,我会重新发布代码审查。我不知道它的存在!谢谢。

标签: java android performance coding-style


【解决方案1】:

由于您调用close() 并返回quote,无论您找到或不是您的线路,您都可以让它变得更好:

 while ((line = buf.readLine()) != null && currLine != lineToFetch) {
        currLine++;
 }
 if (currLine == lineToFetch){
      quote = line;
 }
 randomQuote.close();
 buf.close();
 return quote;

【讨论】:

    【解决方案2】:

    您可以像这样使用 finally 块来简化它 -

    try {
        while ((line = buf.readLine()) != null) {
            // Get a random line number
            if (currLine == lineToFetch) {
                quote = line;
                Log.v("LINE", line);    
                break;
            } else {
                currLine++;
            }
        }
    } finally {
        randomQuote.close();
        buf.close();
    }
    return quote;
    

    【讨论】:

    • 由于randomQuotebuf 似乎实现了AutoCloseable,我更喜欢try-with-resources 而不是try-finally。
    【解决方案3】:

    您可以简单地使用break 退出循环,然后依赖函数末尾的清理代码:

    while ((line = buf.readLine()) != null) {
        // Get a random line number
        if (currLine == lineToFetch) {
            quote = line;
            Log.v("LINE", line);
            break;
        } else
            currLine++;
    }
    randomQuote.close();
    buf.close();
    return quote;
    

    【讨论】:

      【解决方案4】:

      您可以只阅读行,直到找到您想要的行。如果你找到了,那么 line 将是非空的。

      while ((line = buf.readline()) != null && curLine < lineToFetch)
          curLine++;
      
      // make sure we've found the line
      if ( line != null) {
          quote = line;
          Log.v("LINE", line );
      }
      
      randomQote.close();
      buf.close();
      return quote;
      

      【讨论】:

        【解决方案5】:

        基本上,只要找到引用就退出while循环:

            foundQuote = false;
        
            while ((line = buf.readLine()) != null && !foundQuote ) {
              // Get a random line number
              if (currLine == lineToFetch) {
                quote = line;
                foundQuote = true;
            } else
                currLine++;
            }
        
            randomQuote.close();
            buf.close();
            return quote;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-21
          • 1970-01-01
          • 2011-10-14
          • 2016-07-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多