【问题标题】:Java method efficiencyJava方法效率
【发布时间】:2017-09-17 16:09:11
【问题描述】:

如何在一个方法中获取 2 个打印数据?

在下面,看起来我的编码写作不需要写两次。在同一个函数中只想写一篇编码。

public class StringBuffer{
   public static void main(String[] args) {
      countTo_N_Improved(); 
      System.out.println();
      Reverse();
   }

   private final static int MAX_LENGTH = 24;
   private final static int MAX_LENGTH1 = 24;
   private static String buffer = "";
   private static String buffer1 = "";
   private static void emit(String nextChunk) {
       if(buffer.length() + nextChunk.length() > MAX_LENGTH) {
             System.out.println(buffer);
             buffer = "";
       }
           if (nextChunk.length()==2){
               nextChunk = " 0"+nextChunk.trim();
           }
          buffer += nextChunk;      
       }

   private static void emit1(String nextChunk1) {
       if(buffer1.length() + nextChunk1.length() > MAX_LENGTH1) {
             System.out.println(buffer1);
             buffer1 = "";
       }
           if (nextChunk1.length()==2){
               nextChunk1 = " 0"+nextChunk1.trim();
           }
          buffer1 = nextChunk1+buffer1;      
       }

   private static final int N = 100;
   private static void countTo_N_Improved() {
      for (int count=2; count<=N; count=count+2) { 
              emit(" " + count);
          }      
      }

   private static void Reverse() {
          for (int count1=2; count1<=N; count1=count1+2) { 
                  emit1(" " + count1);
              }      
          }
}

输出必须是:

 02 04 06 08 10 12 14 16
 18 20 22 24 26 28 30 32
 34 36 38 40 42 44 46 48
 50 52 54 56 58 60 62 64
 66 68 70 72 74 76 78 80
 82 84 86 88 90 92 94 96

 16 14 12 10 08 06 04 02
 32 30 28 26 24 22 20 18
 48 46 44 42 40 38 36 34
 64 62 60 58 56 54 52 50
 80 78 76 74 72 70 68 66
 96 94 92 90 88 86 84 82

第一个用于实际数据 Max_Length,第二个用于反向。

【问题讨论】:

  • “2 个打印数据”是什么意思?期望的输出是什么?
  • 输出必须是数据源,反转数据源

标签: java string buffer stringbuffer


【解决方案1】:

您确实可以使用参数 reverse 将 countToN 和 emit 重构为单个函数:

public class StringBuffer {
    private final static int MAX_LENGTH = 24;
    private static final int N = 100;
    private static String buffer = "";

    public static void main(String[] args) {
        countToN(false);
        System.out.println();
        countToN(true);
    }

    private static void countToN(boolean reverse) {
        for (int count = 2; count <= N; count = count + 2) {
            emit(" " + count, reverse);
        }
        buffer = "";
    }

    private static void emit(String nextChunk, boolean reverse) {
        if (buffer.length() + nextChunk.length() > MAX_LENGTH) {
            System.out.println(buffer);
            buffer = "";
        }
        if (nextChunk.length() == 2) {
            nextChunk = " 0" + nextChunk.trim();
        }
        buffer = (reverse ? nextChunk + buffer : buffer + nextChunk);
    }
}

【讨论】:

  • 太好了,很高兴能帮上忙。然后请您投票并接受答案以结束问题。
【解决方案2】:

您也可以使用StringBuilder 作为缓冲区。它应该使您的代码更简单、更高效。

public class StringBuffer {

private static final int MAX_LENGTH = 24;
private static final StringBuilder BUFFER = new StringBuilder();
private static final int N = 100;

public static void main(String[] args) {
    StringBuffer.countToN(false);
    System.out.println("");
    StringBuffer.countToN(true);
}

public static void emit(String nextChunk, boolean reverse) {
    if (BUFFER.length() + nextChunk.length() > MAX_LENGTH) {
        System.out.println(BUFFER.toString());
        StringBuffer.BUFFER.delete(0, BUFFER.length());
    }
    if (reverse) {
        StringBuffer.BUFFER.insert(0, nextChunk);
    } else {
        StringBuffer.BUFFER.append(nextChunk);
    }

}

public static void countToN(boolean reverse) {
    for (int count = 2; count <= N; count = count + 2) { 
        String nextChunk = String.format("%02d ", count);
        emit(nextChunk, reverse);
    }   
    StringBuffer.BUFFER.delete(0, BUFFER.length());
}

}


if (nextChunk.length() == 2) {
    nextChunk = " 0" + nextChunk.trim();
}

可以替换为:

String nextChunk = String.format("%02d ", count);

【讨论】:

    猜你喜欢
    • 2015-08-09
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2010-10-28
    • 1970-01-01
    • 2017-04-09
    • 2013-04-18
    相关资源
    最近更新 更多