0

我试图将用户输入的字符串附加到使用全局按键和我找到的 api 中。但是我不知道如何在不覆盖已经存在的情况下添加到字符串中。

int i = 1;
public String log = "Write";
public String[] logger = new String[i];
public static PrintStream out = null;
public BufferedWriter writer = null;




    public void nativeKeyPressed(NativeKeyEvent e) {
        while(i<1000000000){

        logger[i] = NativeKeyEvent.getKeyText(e.getKeyCode());
        i++;
        System.out.println(logger[i]);
        }
4

1 回答 1

2

每当您必须将字符或字符串附加到现有变量时,请使用 StringBuilder 类附加方法。

StringBuilder strBuilder= new StringBuilder();
strBuilder.append("I am a bad programmer");
strBuilder.append("to use concatenation");
strBuilder.append("Instead of StringBuilder");
strBuilder.append(" or StringBuffer");
String str= strBuilder.toString();

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html
于 2013-02-05T00:55:01.883 回答