字符串加法与StringBuilder.append()


public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int times = 1000;// 次数

long beginBuilder = System.currentTimeMillis();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < times; i++) {
builder.append(i);
}
long endBuilder = System.currentTimeMillis();
long useBuilder = endBuilder - beginBuilder;
System.out.println("builder 使用时间:" + useBuilder);

long beginAdd = System.currentTimeMillis();
String str = "";
for (int i = 0; i < times; i++) {
str += i;
}
long endAdd = System.currentTimeMillis();
long useAdd = endAdd - beginAdd;
System.out.println("add 使用时间:" + useAdd);

}
}

执行效率比较如下,
[table]
|times|1000|5000|10000|20000|
|append|0|0|0|0|
|+|31|454|2031|13328|
[/table]
背后的原理是怎样的,还需研究一番[暂时搁浅吧]呀...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在这个程序中,有以下两个问题: 1. 字符串比较应该使用equals方法,而不是==运算符。 2. 在处理加法、撤销和双倍运算时,应该先检查是否存在足够的操作数。 下面是修复了这些问题的代码: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] ops = sc.nextLine().split(" "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ops.length; i++) { if (ops[i].matches("[0-9]+")) { sb.append(ops[i]); } else if (ops[i].equals("+")) { if (sb.length() < 2) { System.out.println("-1"); return; } int a = Integer.parseInt(sb.substring(sb.length() - 2, sb.length() - 1)); int b = Integer.parseInt(sb.substring(sb.length() - 1)); sb.append(a + b); } else if (ops[i].equals("C")) { if (sb.length() < 1) { System.out.println("-1"); return; } sb.deleteCharAt(sb.length() - 1); } else if (ops[i].equals("D")) { if (sb.length() < 1) { System.out.println("-1"); return; } int a = Integer.parseInt(sb.substring(sb.length() - 1)); sb.append(a * 2); } else { System.out.println("-1"); return; } System.out.println(sb.toString()); } } } ``` 优化输出: 在每次处理完操作后,输出当前的得分情况即可,不需要每次都输出所有的操作序列。修改后的代码如下: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] ops = sc.nextLine().split(" "); StringBuilder sb = new StringBuilder(); int sum = 0; for (int i = 0; i < ops.length; i++) { if (ops[i].matches("[0-9]+")) { sb.append(ops[i]); sum += Integer.parseInt(ops[i]); } else if (ops[i].equals("+")) { if (sb.length() < 2) { System.out.println("-1"); return; } int a = Integer.parseInt(sb.substring(sb.length() - 2, sb.length() - 1)); int b = Integer.parseInt(sb.substring(sb.length() - 1)); sb.append(a + b); sum += a + b; } else if (ops[i].equals("C")) { if (sb.length() < 1) { System.out.println("-1"); return; } sum -= Integer.parseInt(sb.substring(sb.length() - 1)); sb.deleteCharAt(sb.length() - 1); } else if (ops[i].equals("D")) { if (sb.length() < 1) { System.out.println("-1"); return; } int a = Integer.parseInt(sb.substring(sb.length() - 1)); sb.append(a * 2); sum += a * 2; } else { System.out.println("-1"); return; } System.out.println(sum); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值